1.什麼是rss?
RSS 的全稱是「簡易內容聚合」(Really Simple Syndication),是一個能讓你在一個地方訂閱各種感興趣網站的工具。
一個網站支援 RSS,就意味著每當它新發布一篇新文章,就會往一個位於特定網址的檔案中,以特定的語法(具體而言是 XML 標記語言或 JSON)增加一條記錄,列明這篇文章的標題、作者、發表時間和內容(可以是全文,也可以是摘要)等資訊。這樣,使用者只要蒐集所有他感興趣的網站提供的這種檔案的網址,並不時檢查這些檔案內容的更新,就能知道這些網站是否、何時釋出了什麼內容。RSS 閱讀器的核心功能,就是儲存使用者訂閱的 RSS 地址,以固定的頻率自動檢查更新,並將其內容轉換為易讀的格式呈現給使用者。
為什麼用 RSS
RSS 的對立面是演算法推薦,像微信公眾號、知乎、微博、今日頭條等平臺。 且不說演算法推送平臺廣告多,遷移麻煩的問題。演算法推薦的特點是,你不需要刻意選擇,演算法會根據你的喜好,給你推送內容。這樣一來,你幾乎沒有選擇的餘地,在不斷被「餵飽」中逐漸失去判斷的能力。更可怕的地方在於,它替你定義了你的畫像,然後把你潛移默化中變成了它所認為的你。「大資料殺熟」的東窗事發絕非偶然,用演算法窺視使用者隱私是當今網際網路公司的通配。
做資訊的主人,而不是奴隸。RSS 是一種公開的協議,可自由更換平臺與客戶端。重要的一點是,獲取資訊的權力完全自治。RSS 相比演算法推薦,擁有了可控性和安全感,隱私完全掌握在自己手裏。
什麼是atom?
Atom是一對彼此相關的標準。Atom供稿格式(Atom Syndication Format)是用於網站訊息來源,基於XML的文件格式;而Atom出版協定(Atom Publishing Protocol,簡稱AtomPub或APP)是用於新增及修改網路資源,基於HTTP的協議。
它借鑑了各種版本RSS的使用經驗,被許多的聚合工具廣泛使用在釋出和使用上。Atom供稿格式設計作為RSS的替代品;而Atom出版協定用來取 代現有的多種釋出方式(如Blogger API和LiveJournal XML-RPC Client/Server Protocol)。而值得一提的是Google提供的多種服務正在使用Atom。Google Data API(GData)亦基於Atom。
Atom是IETF的"建議標準",Atom供稿格式列為RFC 4287,而Atom出版協定列為RFC 5023。
2.程式碼工程
實驗目標:實現rss和atom訂閱源
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>rss</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--ROAM依賴 RSS 訂閱--> <dependency> <groupId>com.rometools</groupId> <artifactId>rome</artifactId> <version>1.15.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.rometools/rome-utils --> <dependency> <groupId>com.rometools</groupId> <artifactId>rome-utils</artifactId> <version>1.15.0</version> </dependency> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom2</artifactId> <version>2.0.6</version> </dependency> </dependencies> </project>
controller
採用rome庫實現rss和atom訂閱
package com.et.rss.controller; import com.rometools.rome.feed.atom.*; import com.rometools.rome.feed.rss.Channel; import com.rometools.rome.feed.rss.Description; import com.rometools.rome.feed.rss.Image; import com.rometools.rome.feed.rss.Item; import com.rometools.rome.feed.synd.SyndPerson; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Collections; import java.util.Date; @RestController @RequestMapping("/feed") public class FeedController { @GetMapping(path = "/rss") public Channel rss() { Channel channel = new Channel(); channel.setFeedType("rss_2.0"); channel.setTitle("HBLOG Feed"); channel.setDescription("Recent posts"); channel.setLink("http://www.liuhaihua.cn"); channel.setUri("http://www.liuhaihua.cn"); channel.setGenerator("Harries"); Image image = new Image(); image.setUrl("http://www.liuhaihua.cn/img/hblog.png"); image.setTitle("HBLOG Feed"); image.setHeight(32); image.setWidth(32); channel.setImage(image); Date postDate = new Date(); channel.setPubDate(postDate); Item item = new Item(); item.setAuthor("Harries"); item.setLink("http://www.liuhaihua.cn/archives/710608.html"); item.setTitle("Spring Boot integrated banner quick start demo"); item.setUri("http://www.liuhaihua.cn/archives/710608.html"); item.setComments("http://www.liuhaihua.cn/archives/710608.html#commnet"); com.rometools.rome.feed.rss.Category category = new com.rometools.rome.feed.rss.Category(); category.setValue("CORS"); item.setCategories(Collections.singletonList(category)); Description descr = new Description(); descr.setValue("pring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information"); item.setDescription(descr); item.setPubDate(postDate); channel.setItems(Collections.singletonList(item)); //Like more Entries here about different new topics return channel; } @GetMapping(path = "/atom") public Feed atom() { Feed feed = new Feed(); feed.setFeedType("atom_1.0"); feed.setTitle("HBLOG"); feed.setId("http://www.liuhaihua.cn"); Content subtitle = new Content(); subtitle.setType("text/plain"); subtitle.setValue("recents post"); feed.setSubtitle(subtitle); Date postDate = new Date(); feed.setUpdated(postDate); Entry entry = new Entry(); Link link = new Link(); link.setHref("http://www.liuhaihua.cn/archives/710608.html"); entry.setAlternateLinks(Collections.singletonList(link)); SyndPerson author = new Person(); author.setName("HBLOG"); entry.setAuthors(Collections.singletonList(author)); entry.setCreated(postDate); entry.setPublished(postDate); entry.setUpdated(postDate); entry.setId("710608"); entry.setTitle("Spring Boot integrated banner quick start demo"); Category category = new Category(); category.setTerm("CORS"); entry.setCategories(Collections.singletonList(category)); Content summary = new Content(); summary.setType("text/plain"); summary.setValue("Spring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information"); entry.setSummary(summary); feed.setEntries(Collections.singletonList(entry)); //add different topic return feed; } }
DemoApplication.java
package com.et.rss; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
application.yml
server: port: 8088
以上只是一些關鍵程式碼,所有程式碼請參見下面程式碼倉庫
程式碼倉庫
https://github.com/Harries/springboot-demo
3.測試
啟動Spring Boot應用
rss
http://127.0.0.1:8088/feed/rss
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel> <title>HBLOG Feed</title> <link>http://www.codeuphub.com</link> <description>Recent posts</description> <pubDate>Fri, 24 May 2024 14:26:21 GMT</pubDate> <generator>Harries</generator> <image> <title>codeup Feed</title> <url>https://www.codeuphub.com/upload/USER_1_TB_MAIN/2024051517157109845767351.png</url> <width>32</width> <height>32</height> </image> <item> <title>Spring Boot integrated banner quick start demo</title> <link>https://www.codeuphub.com/tw/front/template/157/view/article</link> <description>pring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information</description> <category>CORS</category> <pubDate>Fri, 24 May 2024 14:26:21 GMT</pubDate> <author>Harries</author> <comments>https://www.codeuphub.com/tw/front/template/157/view/article#clickTarget</comments> </item> </channel> </rss> atom http://127.0.0.1:8088/feed/atom <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>HBLOG</title> <subtitle type="text">recents post</subtitle> <id>http://www.codeuphub.com</id> <updated>2024-05-24T14:25:38Z</updated> <entry> <title>Spring Boot integrated banner quick start demo</title> <link rel="alternate" href="https://www.codeuphub.com/tw/front/template/157/view/article" /> <category term="CORS" /> <author> <name>codeup</name> </author> <id>710608</id> <updated>2024-05-24T14:25:38Z</updated> <published>2024-05-24T14:25:38Z</published> <summary type="text">Spring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information</summary> </entry> </feed>
4.引用
https://howtodoinjava.com/spring-boot/spring-boot-rome-rss-and-atom-feed/