一、导入依赖
<!-- RSS start -->
<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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- RSS end -->
二、直接调用
@Component
@Slf4j
public class RssUtils {
public List<BlogDetailModel> getBlogInfo(String blogAddress){
List<BlogDetailModel> blogDetailModelList = new ArrayList<>();
try {
URL feedUrl = new URL(blogAddress);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = null;
feed = input.build(new XmlReader(feedUrl));
if (feed == null) {
log.error("feed is null");
}
List<SyndEntry> list = feed.getEntries();
for (SyndEntry entry : list) {
BlogDetailModel blogDetailModel = new BlogDetailModel();
blogDetailModel.setBlogTitle(entry.getTitle());
blogDetailModel.setBlogLink(entry.getLink());
blogDetailModel.setAuthor(entry.getAuthor());
blogDetailModelList.add(blogDetailModel);
}
}catch(Exception e) {
e.printStackTrace();
}
return blogDetailModelList;
}
}