引言
在本教程中,學習從spring boot應用程序創建和使用RSS和Atom提要。 您必須在各種網站上(例如我們的RSS提要)以文本或圖像按鈕的形式看到這一點,邀請您“通過RSS訂閱”。RSS是簡單的聯合API - 通常稱爲Rich Site Summary。 RSS革新了用戶與在線內容交互的方式。
與RSS類似,Atom也是基於XML的Web內容和元數據聯合格式,以及用於發佈和編輯屬於定期更新網站的Web資源的應用程序級協議。 所有Atom提要必須是格式良好的XML文檔,應用程序/ atom + xml媒體類型。
一.概述
在這個spring boot例子中,我們將使用spring bootAPI公開兩個簡單的RSS和ATOM端點,我們將看到如何使用java客戶端來使用這些訂閱。
二.環境準備
- JDK 1.8, Eclipse, Maven – 開發環境
- Spring-boot – 底層的應用程序框架
- ROME library – 用於發佈Feed
三.如何實現?
基本上用Spring框架發佈RSS或Atom提要非常簡單。 在Spring框架中有兩個http消息轉換器(RssChannelHttpMessageConverter和AtomFeedHttpMessageConverter),可以將spring控制器方法的響應轉換爲XML feed格式,如果返回類型與任何這些feed有關。
兩個轉換器都依賴於ROME庫,當它發現類路徑中的庫時,Spring Framework會自動註冊這兩個轉換器。 我們所要做的就是將ROME庫作爲依賴添加到我們的pom.xml中。
3.1 創建spring boot項目
Spring Boot 項目創建可以參考:Spring Boot 入門 創建項目
3.2添加ROAM依賴
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome</artifactId>
<version>1.8.0</version>
</dependency>
3.2創建一個controller
現在添加一個Spring Controller,並分別添加兩個端點/ rss和/ atom來公開RSS和Atom提要。 正如我們已經提到的,只是添加這個控制器會自動工作的情況下,因爲內部彈簧框架將註冊兩個http消息轉換器(RssChannelHttpMessageConverter和AtomFeedHttpMessageConverter),一旦我們在類路徑中有ROAM依賴關係,將被註冊。
只有我們需要這樣做才能從控制器方法返回適當的Feed類型對象。 在我們的例子中,feed對象的類型是com.rometools.rome.feed.rss.Channel,而Atom則是com.rometools.rome.feed.atom.Feed。 所以在添加Feed的內容和有關頻道的其他細節之後,我們的控制器將如下所示。
package net.xqlee.project.controller;
import java.util.Collections;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rometools.rome.feed.atom.Category;
import com.rometools.rome.feed.atom.Content;
import com.rometools.rome.feed.atom.Entry;
import com.rometools.rome.feed.atom.Feed;
import com.rometools.rome.feed.atom.Link;
import com.rometools.rome.feed.atom.Person;
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;
@RestController
public class FeedController {
@GetMapping(path = "/rss")
public Channel rss() {
Channel channel = new Channel();
channel.setFeedType("rss_2.0");
channel.setTitle("Leftso Feed");
channel.setDescription("最新技術的不同文章");
channel.setLink("http://www.leftso.com");
channel.setUri("http://www.leftso.com");
channel.setGenerator("在屋裏編程");
Image image = new Image();
image.setUrl("/resources/assist/images/blog/b8fb228cdff44b8ea65d1e557bf05d2b.png");
image.setTitle("Leftso Feed");
image.setHeight(32);
image.setWidth(32);
channel.setImage(image);
Date postDate = new Date();
channel.setPubDate(postDate);
Item item = new Item();
item.setAuthor("Leftso");
item.setLink("http://www.leftso.com/blog/64.html");
item.setTitle("http://www.leftso.com/blog/64.html");
item.setUri("http://www.leftso.com/blog/64.html");
item.setComments("http://www.leftso.com/blog/64.html");
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(
"兩者沒有必然的聯繫,但是spring boot可以看作爲spring MVC的升級版."
+ " <a rel=\"nofollow\" href=\"http://www.leftso.com/blog/64.html/\">Spring boot 入門(一)環境搭建以及第一個應用</a>發佈在 <a rel=\"nofollow\" href=\"http://www.leftso.com\">Leftso</a>.");
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("Leftso");
feed.setId("http://www.leftso.com/");
Content subtitle = new Content();
subtitle.setType("text/plain");
subtitle.setValue("最新技術的不同文章");
feed.setSubtitle(subtitle);
Date postDate = new Date();
feed.setUpdated(postDate);
Entry entry = new Entry();
Link link = new Link();
link.setHref("http://www.leftso.com/blog/64.html");
entry.setAlternateLinks(Collections.singletonList(link));
SyndPerson author = new Person();
author.setName("Leftso");
entry.setAuthors(Collections.singletonList(author));
entry.setCreated(postDate);
entry.setPublished(postDate);
entry.setUpdated(postDate);
entry.setId("http://www.leftso.com/blog/64.html");
entry.setTitle("Spring boot 入門(一)環境搭建以及第一個應用");
Category category = new Category();
category.setTerm("CORS");
entry.setCategories(Collections.singletonList(category));
Content summary = new Content();
summary.setType("text/plain");
summary.setValue("兩者沒有必然的聯繫,但是spring boot可以看作爲spring MVC的升級版."
+ " <a rel=\"nofollow\" href=\"http://www.leftso.com/blog/64.html/\">Spring boot 入門(一)環境搭建以及第一個應用</a>發佈在 <a rel=\"nofollow\" href=\"http://www.leftso.com\">Leftso</a>.");
entry.setSummary(summary);
feed.setEntries(Collections.singletonList(entry));
//參加這裏關於不同的新話題
return feed;
}
}
3.5創建RSS Feed閱讀器
我們已經有很多Feed閱讀器可用,但是如果您需要以編程方式使用此Feed,則可以通過使用ROAM庫的這幾行簡單代碼來完成此操作。
package net.xqlee.project;
import java.net.URL;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
public class FeedConsumer {
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();
}
}
}
這裏的URL是用於RSS提要的,如果將URL更改爲Atom提要,同樣的代碼也可以工作。
輸出
這裏是控制檯輸出feed client:
Leftso Feed
***********************************
SyndEntryImpl.comments=http://www.leftso.com/blog/64.html
SyndEntryImpl.author=Leftso
SyndEntryImpl.wireEntry=null
SyndEntryImpl.link=http://www.leftso.com/blog/64.html
SyndEntryImpl.description.mode=null
SyndEntryImpl.description.type=text/html
SyndEntryImpl.description.interface=interface com.rometools.rome.feed.synd.SyndContent
SyndEntryImpl.description.value=兩者沒有必然的聯繫,但是spring boot可以看作爲spring MVC的升級版. <a rel="nofollow" href="http://www.leftso.com/blog/64.html/">Spring boot 入門(一)環境搭建以及第一個應用</a>發佈在 <a rel="nofollow" href="http://www.leftso.com">Leftso</a>.
SyndEntryImpl.foreignMarkup=[]
SyndEntryImpl.updatedDate=null
SyndEntryImpl.source=null
SyndEntryImpl.title=http://www.leftso.com/blog/64.html
SyndEntryImpl.interface=interface com.rometools.rome.feed.synd.SyndEntry
SyndEntryImpl.uri=http://www.leftso.com/blog/64.html
SyndEntryImpl.enclosures=[]
SyndEntryImpl.modules[0].date=Sun Nov 12 11:57:14 CST 2017
SyndEntryImpl.modules[0].formats=[]
SyndEntryImpl.modules[0].rightsList=[]
SyndEntryImpl.modules[0].sources=[]
SyndEntryImpl.modules[0].creators[0]=Leftso
SyndEntryImpl.modules[0].subject=null
SyndEntryImpl.modules[0].description=null
SyndEntryImpl.modules[0].language=null
SyndEntryImpl.modules[0].source=null
SyndEntryImpl.modules[0].type=null
SyndEntryImpl.modules[0].title=null
SyndEntryImpl.modules[0].interface=interface com.rometools.rome.feed.module.DCModule
SyndEntryImpl.modules[0].coverages=[]
SyndEntryImpl.modules[0].descriptions=[]
SyndEntryImpl.modules[0].relation=null
SyndEntryImpl.modules[0].contributor=null
SyndEntryImpl.modules[0].rights=null
SyndEntryImpl.modules[0].publishers=[]
SyndEntryImpl.modules[0].coverage=null
SyndEntryImpl.modules[0].identifier=null
SyndEntryImpl.modules[0].creator=Leftso
SyndEntryImpl.modules[0].types=[]
SyndEntryImpl.modules[0].languages=[]
SyndEntryImpl.modules[0].identifiers=[]
SyndEntryImpl.modules[0].subjects=[]
SyndEntryImpl.modules[0].format=null
SyndEntryImpl.modules[0].dates[0]=Sun Nov 12 11:57:14 CST 2017
SyndEntryImpl.modules[0].titles=[]
SyndEntryImpl.modules[0].uri=http://purl.org/dc/elements/1.1/
SyndEntryImpl.modules[0].publisher=null
SyndEntryImpl.modules[0].contributors=[]
SyndEntryImpl.modules[0].relations=[]
SyndEntryImpl.contents=[]
SyndEntryImpl.links=[]
SyndEntryImpl.contributors=[]
SyndEntryImpl.publishedDate=Sun Nov 12 11:57:14 CST 2017
SyndEntryImpl.categories[0].taxonomyUri=null
SyndEntryImpl.categories[0].name=CORS
SyndEntryImpl.categories[0].interface=interface com.rometools.rome.feed.synd.SyndCategory
SyndEntryImpl.authors=[]
SyndEntryImpl.titleEx.mode=null
SyndEntryImpl.titleEx.type=null
SyndEntryImpl.titleEx.interface=interface com.rometools.rome.feed.synd.SyndContent
SyndEntryImpl.titleEx.value=http://www.leftso.com/blog/64.html
***********************************
Done