版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32447301/article/details/81750657
一、首先介绍什么RSS、ROME。
RSS:
RSS订阅是站点用来和其他站点之间共享内容的一种简易方式,即Really Simple Syndication(简易信息聚合)。
RSS以其方便快捷的工作方式,为广大网编带了工作效率的跨越,但是也助长了信息高速重复。
ROME:
Rome是为RSS聚合而开发的一个框架,让你可以快速的开发基于java的RSS阅读。
二、使用Springboot开发RSS订阅
1、导入jar包,配置pom.xml
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>1.0</version>
</dependency>
2、编写代码
package com.citydo.rss.utils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
public class FeedConsumer {
private final static String RSS_URL = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=%s&type=&dateb=&owner=exclude&count=&output=atom";
public void test() throws MalformedURLException {
//URL feedUrl = new URL(String.format(RSS_URL, symbol));
//null 代表header
URL feedUrl = new URL(String.format(RSS_URL, null));
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = null;
try {
feed = input.build(new XmlReader(feedUrl));
}catch(Exception e) {
e.printStackTrace();
}
if (feed == null) {
//log.warn("syndFeed is null, symbol:{}", symbol);
return;
}
List<SyndEntry> list = feed.getEntries();
for (SyndEntry entry : list) {
System.out.println(entry.getTitle() + "-"+ entry.getUpdatedDate() + "-" + entry.getLink());
}
}
public static void main(String[] args) {
try {
String url = "http://localhost:8080/rss";
try (XmlReader reader = new XmlReader(new URL(url))) {
SyndFeed feed = new SyndFeedInput().build(reader);
System.out.println(feed.getTitle());
System.out.println("***********************************");
for (SyndEntry entry : feed.getEntries()) {
System.out.println(entry);
System.out.println("***********************************");
}
System.out.println("Done");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}