java 用rome实现 RSS订阅Demo
复制XML<!--使用rome实现RSS 订阅-->
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome</artifactId>
<version>1.8.0</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
复制Javapackage com.leixing.blog.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.rometools.rome.feed.rss.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @explain RSS订阅Demo
* @author luolei
* @date 2020年02月29日
*/
@RestController
public class FeedController {
@GetMapping(path = "/feed")
public Channel createXml() {
Channel channel = new Channel();
channel.setFeedType("rss_2.0");
channel.setTitle("累行客");// 网站标题
channel.setDescription("累行方能前行");// 网站描述
channel.setLink("http://www.leixing.xyz/");// 网站主页链接
channel.setEncoding("utf-8");// RSS文件编码
channel.setLanguage("zh-cn");// RSS使用的语言
channel.setTtl(60);// time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)
channel.setCopyright("©2019-2020 累行客");// 版权声明
channel.setPubDate(new Date());// RSS发布时间
channel.setGenerator("luolei");
List<Item> items = new ArrayList<Item>();// 这个list对应rss中的item列表
for(int i = 0; i < 10; i ++) {
Item item = new Item();// 新建Item对象,对应rss中的<item></item>
item.setTitle("累行客 RSS订阅 "+i);// 对应<item>中的<title></title>
item.setAuthor("luolei"+i);
String url = "http://www.leixing.xyz/"+i;
item.setLink(url); //对应 <item>中的<link></link> 有的会去重
Guid guid = new Guid();// 为当前新闻指定一个全球唯一标示,这个不是必须的
guid.setValue(url);
item.setGuid(guid);
//新建一个Description,它是Item的描述部分
Description description = new Description();
description.setType("text/html");
String str = "<p>RSS基于XML标准,在互联网上被广泛采用的内容包装和投递协议。RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,是使用最广泛的XML应用。<a href='http://www.leixing.xyz'>»查看详情</a></p>";
description.setValue(str);// <description>中的内容
item.setDescription(description);// 添加到item节点中
items.add(item);// 代表一个段落<item></item>,
}
channel.setItems(items);
return channel;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55