https://howtodoinjava.com/spring-boot/spring-boot-rome-rss-and-atom-feed/

在本教程中,学习从 SpringBoot 应用程序创建和使用 RSS 和 Atom 提要。 您一定已经在各种网站(如 RSS 提要之类的)上以文本或图像按钮的形式看到了这一点,并邀请您“通过 RSS 订阅”。RSS 是简单的联合 API,通常称为富站点摘要。 RSS 彻底改变了用户与在线内容进行交互的方式。

与 RSS 相似,Atom 也是基于 XML 的 Web 内容和元数据联合格式,并且是用于发布和编辑属于定期更新的网站的 Web 资源的应用程序级协议。 所有 Atom 提要必须是格式为application/atom+xml的 XML 文档。

总览

在这个运行示例中,我们将使用 Spring Boot API 公开 RSS 和 ATOM 的两个简单端点,并且我们将了解如何使用 java 客户端使用这些提要。

技术栈

  • JDK 1.8,Eclipse,Maven – 开发环境
  • SpringBoot – 基础应用程序框架
  • ROME 库 – 用于发布提要

怎么运行的?

基本上,使用 Spring 框架发布 RSS 或 Atom 提要非常容易。 在 spring 框架中,有两个 http 消息转换器(RssChannelHttpMessageConverterAtomFeedHttpMessageConverter)可以将 spring 控制器方法的响应转换为 XML feed 格式(如果返回类型与以下任何一种相关) 饲料。

这两个转换器都依赖于 ROME 库,当 Spring 框架在类路径上发现库时,Spring 框架会自动注册这两个转换器。 我们要做的就是将 ROME 库添加为pom.xml的依赖。

项目结构

下面给出了为此演示创建的类和文件。

Spring Boot RSS feed 和 ROAM - 图1

项目

创建 RSS / ATOM 提要生成器

创建 Spring Boot 项目

首先从 Spring 初始化器站点创建一个仅具有Web依赖项的 spring boot 项目。 选择依赖项并提供适当的 Maven GAV 坐标后,以压缩格式下载项目。 解压缩,然后将 eclipse 中的项目导入为 maven 项目。

Spring Boot RSS feed 和 ROAM - 图2

Spring boot 项目生成

添加 ROAM 依赖关系

现在,我们需要在新创建的项目的pom.xml中添加 ROAM 依赖项。

  1. <dependency>
  2. <groupId>com.rometools</groupId>
  3. <artifactId>rome</artifactId>
  4. <version>1.8.0</version>
  5. </dependency>

创建控制器

现在添加一个 Spring 控制器并添加两个端点/rss/atom分别公开 RSS 和 Atom 提要。 正如我们已经提到的,仅添加此控制器将自动适用于我们的情况,因为内部 spring 框架将注册两个 http 消息转换器(RssChannelHttpMessageConverterAtomFeedHttpMessageConverter),一旦我们在类路径中具有 ROAM 依赖项,它们便将被注册。

我们唯一需要做的就是从控制器方法中返回正确的Feed类型的对象。 在我们的情况下,提要对象的 RSS 类型为com.rometools.rome.feed.rss.Channel,Atom 类型为com.rometools.rome.feed.atom.Feed。 因此,在添加提要的内容以及有关通道的其他详细信息之后,我们的控制器将如下所示。

  1. package com.example.howtodoinjava.rss;
  2. import java.util.Collections;
  3. import java.util.Date;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.rometools.rome.feed.atom.Category;
  7. import com.rometools.rome.feed.atom.Content;
  8. import com.rometools.rome.feed.atom.Entry;
  9. import com.rometools.rome.feed.atom.Feed;
  10. import com.rometools.rome.feed.atom.Link;
  11. import com.rometools.rome.feed.atom.Person;
  12. import com.rometools.rome.feed.rss.Channel;
  13. import com.rometools.rome.feed.rss.Description;
  14. import com.rometools.rome.feed.rss.Image;
  15. import com.rometools.rome.feed.rss.Item;
  16. import com.rometools.rome.feed.synd.SyndPerson;
  17. @RestController
  18. public class FeedController {
  19. @GetMapping(path = "/rss")
  20. public Channel rss() {
  21. Channel channel = new Channel();
  22. channel.setFeedType("rss_2.0");
  23. channel.setTitle("HowToDoInJava Feed");
  24. channel.setDescription("Different Articles on latest technology");
  25. channel.setLink("https://howtodoinjava.com");
  26. channel.setUri("https://howtodoinjava.com");
  27. channel.setGenerator("In House Programming");
  28. Image image = new Image();
  29. image.setUrl("https://howtodoinjava.com/wp-content/uploads/2015/05/howtodoinjava_logo-55696c1cv1_site_icon-32x32.png");
  30. image.setTitle("HowToDoInJava Feed");
  31. image.setHeight(32);
  32. image.setWidth(32);
  33. channel.setImage(image);
  34. Date postDate = new Date();
  35. channel.setPubDate(postDate);
  36. Item item = new Item();
  37. item.setAuthor("Lokesh Gupta");
  38. item.setLink("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/");
  39. item.setTitle("Spring CORS Configuration Examples");
  40. item.setUri("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/");
  41. item.setComments("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/#respond");
  42. com.rometools.rome.feed.rss.Category category = new com.rometools.rome.feed.rss.Category();
  43. category.setValue("CORS");
  44. item.setCategories(Collections.singletonList(category));
  45. Description descr = new Description();
  46. descr.setValue(
  47. "CORS helps in serving web content from multiple domains into browsers who usually have the same-origin security policy. In this example, we will learn to enable CORS support in Spring MVC application at method and global level."
  48. + "The post <a rel=\"nofollow\" href=\"https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/\">Spring CORS Configuration Examples</a> appeared first on <a rel=\"nofollow\" href=\"https://howtodoinjava.com\">HowToDoInJava</a>.");
  49. item.setDescription(descr);
  50. item.setPubDate(postDate);
  51. channel.setItems(Collections.singletonList(item));
  52. //Like more Entries here about different new topics
  53. return channel;
  54. }
  55. @GetMapping(path = "/atom")
  56. public Feed atom() {
  57. Feed feed = new Feed();
  58. feed.setFeedType("atom_1.0");
  59. feed.setTitle("HowToDoInJava");
  60. feed.setId("https://howtodoinjava.com");
  61. Content subtitle = new Content();
  62. subtitle.setType("text/plain");
  63. subtitle.setValue("Different Articles on latest technology");
  64. feed.setSubtitle(subtitle);
  65. Date postDate = new Date();
  66. feed.setUpdated(postDate);
  67. Entry entry = new Entry();
  68. Link link = new Link();
  69. link.setHref("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/");
  70. entry.setAlternateLinks(Collections.singletonList(link));
  71. SyndPerson author = new Person();
  72. author.setName("Lokesh Gupta");
  73. entry.setAuthors(Collections.singletonList(author));
  74. entry.setCreated(postDate);
  75. entry.setPublished(postDate);
  76. entry.setUpdated(postDate);
  77. entry.setId("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/");
  78. entry.setTitle("spring-mvc-cors-configuration");
  79. Category category = new Category();
  80. category.setTerm("CORS");
  81. entry.setCategories(Collections.singletonList(category));
  82. Content summary = new Content();
  83. summary.setType("text/plain");
  84. summary.setValue(
  85. "CORS helps in serving web content from multiple domains into browsers who usually have the same-origin security policy. In this example, we will learn to enable CORS support in Spring MVC application at method and global level."
  86. + "The post <a rel=\"nofollow\" href=\"https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/\">Spring CORS Configuration Examples</a> appeared first on <a rel=\"nofollow\" href=\"https://howtodoinjava.com\">HowToDoInJava</a>.");
  87. entry.setSummary(summary);
  88. feed.setEntries(Collections.singletonList(entry));
  89. //Like more Entries here about different new topics
  90. return feed;
  91. }
  92. }

示例

启动 spring boot 应用程序,使用mvn clean install进行 maven 构建,然后使用java -jar target\spring-boot-rss-feed-example-0.0.1-SNAPSHOT.jar命令启动应用程序。 这将在默认端口8080中启动一台 tomcat 服务器,并将在其中部署应用程序。

现在,从浏览器转到 http://localhost:8080/rsshttp://localhost:8080/atom,您应该会看到 RSS 和 Atom 提要中的主题已添加到控制器中。

Spring Boot RSS feed 和 ROAM - 图3

RSS feed

Spring Boot RSS feed 和 ROAM - 图4

Atom Feed

创建 RSS Feed 阅读器

我们已经有很多 Feed 阅读器可用,但是如果您需要以编程方式使用此 Feed,也可以使用 ROAM 库通过以下几行代码来完成此操作。

  1. package com.example.howtodoinjava.rss;
  2. import java.net.URL;
  3. import com.rometools.rome.feed.synd.SyndEntry;
  4. import com.rometools.rome.feed.synd.SyndFeed;
  5. import com.rometools.rome.io.SyndFeedInput;
  6. import com.rometools.rome.io.XmlReader;
  7. public class FeedConsumer {
  8. public static void main(String[] args) {
  9. try {
  10. String url = "http://localhost:8080/rss";
  11. try (XmlReader reader = new XmlReader(new URL(url))) {
  12. SyndFeed feed = new SyndFeedInput().build(reader);
  13. System.out.println(feed.getTitle());
  14. System.out.println("***********************************");
  15. for (SyndEntry entry : feed.getEntries()) {
  16. System.out.println(entry);
  17. System.out.println("***********************************");
  18. }
  19. System.out.println("Done");
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }

这里的 URL 用于 RSS feed,如果将 URL 更改为 Atom feed,同样的代码也可以工作。

输出

这是 feed 客户端的控制台输出。

  1. HowToDoInJava Feed
  2. ***********************************
  3. SyndEntryImpl.comments=https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/#respond
  4. SyndEntryImpl.author=Lokesh Gupta
  5. SyndEntryImpl.wireEntry=null
  6. SyndEntryImpl.link=https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/
  7. SyndEntryImpl.description.mode=null
  8. SyndEntryImpl.description.type=text/html
  9. SyndEntryImpl.description.interface=interface com.rometools.rome.feed.synd.SyndContent
  10. SyndEntryImpl.description.value=CORS helps in serving web content from multiple domains into browsers who usually have the same-origin security policy. In this example, we will learn to enable CORS support in Spring MVC application at method and global level.The post <a rel="nofollow" href="https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/">Spring CORS Configuration Examples</a> appeared first on <a rel="nofollow" href="https://howtodoinjava.com">HowToDoInJava</a>.
  11. SyndEntryImpl.foreignMarkup=[]
  12. SyndEntryImpl.source=null
  13. SyndEntryImpl.updatedDate=null
  14. SyndEntryImpl.title=Spring CORS Configuration Examples
  15. SyndEntryImpl.interface=interface com.rometools.rome.feed.synd.SyndEntry
  16. SyndEntryImpl.uri=https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/
  17. SyndEntryImpl.enclosures=[]
  18. SyndEntryImpl.modules[0].date=Sat Oct 14 10:57:12 IST 2017
  19. SyndEntryImpl.modules[0].formats=[]
  20. SyndEntryImpl.modules[0].sources=[]
  21. SyndEntryImpl.modules[0].rightsList=[]
  22. SyndEntryImpl.modules[0].subject=null
  23. SyndEntryImpl.modules[0].creators[0]=Lokesh Gupta
  24. SyndEntryImpl.modules[0].description=null
  25. SyndEntryImpl.modules[0].language=null
  26. SyndEntryImpl.modules[0].source=null
  27. SyndEntryImpl.modules[0].type=null
  28. SyndEntryImpl.modules[0].title=null
  29. SyndEntryImpl.modules[0].interface=interface com.rometools.rome.feed.module.DCModule
  30. SyndEntryImpl.modules[0].descriptions=[]
  31. SyndEntryImpl.modules[0].coverages=[]
  32. SyndEntryImpl.modules[0].relation=null
  33. SyndEntryImpl.modules[0].contributor=null
  34. SyndEntryImpl.modules[0].rights=null
  35. SyndEntryImpl.modules[0].publishers=[]
  36. SyndEntryImpl.modules[0].coverage=null
  37. SyndEntryImpl.modules[0].identifier=null
  38. SyndEntryImpl.modules[0].creator=Lokesh Gupta
  39. SyndEntryImpl.modules[0].types=[]
  40. SyndEntryImpl.modules[0].languages=[]
  41. SyndEntryImpl.modules[0].identifiers=[]
  42. SyndEntryImpl.modules[0].subjects=[]
  43. SyndEntryImpl.modules[0].format=null
  44. SyndEntryImpl.modules[0].dates[0]=Sat Oct 14 10:57:12 IST 2017
  45. SyndEntryImpl.modules[0].titles=[]
  46. SyndEntryImpl.modules[0].uri=http://purl.org/dc/elements/1.1/
  47. SyndEntryImpl.modules[0].publisher=null
  48. SyndEntryImpl.modules[0].contributors=[]
  49. SyndEntryImpl.modules[0].relations=[]
  50. SyndEntryImpl.contents=[]
  51. SyndEntryImpl.links=[]
  52. SyndEntryImpl.publishedDate=Sat Oct 14 10:57:12 IST 2017
  53. SyndEntryImpl.contributors=[]
  54. SyndEntryImpl.categories[0].taxonomyUri=null
  55. SyndEntryImpl.categories[0].name=CORS
  56. SyndEntryImpl.categories[0].interface=interface com.rometools.rome.feed.synd.SyndCategory
  57. SyndEntryImpl.titleEx.mode=null
  58. SyndEntryImpl.titleEx.type=null
  59. SyndEntryImpl.titleEx.interface=interface com.rometools.rome.feed.synd.SyndContent
  60. SyndEntryImpl.titleEx.value=Spring CORS Configuration Examples
  61. SyndEntryImpl.authors=[]
  62. ***********************************
  63. Done

总结

在此示例中,我们了解到如何轻松地将 RSS 和 Atom feed 配置到我们的 spring boot 项目中。 我们也看到了如何从 Java 代码中使用它们。 就是今天的话题。 我在这里附上完整的 Eclipse 项目,以供您参考。

请在评论部分添加您的反馈。

下载源码

学习愉快!