Display RSS Feed with Spring MVC – 用Spring MVC显示RSS提要

最后修改: 2018年 7月 30日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

This quick tutorial will show how to build a simple RSS feed using Spring MVC and the AbstractRssFeedView class.

这个快速教程将展示如何使用Spring MVC和AbstractRssFeedView类建立一个简单的RSS feed。

Afterward, we’ll also implement a simple REST API – to expose our feed over the wire.

之后,我们还将实现一个简单的REST API–通过电线暴露我们的饲料。

2. RSS Feed

2.RSS馈送

Before going into the implementation details, let’s make a quick review on what RSS is and how it works.

在进入实施细节之前,让我们快速回顾一下什么是RSS,它是如何工作的。

RSS is a type of web feed which easily allows a user to keep track of updates from a website. Furthermore, RSS feeds are based on an XML file which summarizes the content of a site. A news aggregator can then subscribe to one or more feeds and display the updates by regularly checking if the XML has changed.

RSS是一种网络提要,它可以轻松地让用户跟踪一个网站的更新。此外,RSS提要是基于一个总结了网站内容的XML文件。然后一个新闻聚合器可以订阅一个或多个提要,并通过定期检查XML是否有变化来显示更新。

3. Dependencies

3.依赖性

First of all, since Spring’s RSS support is based on the ROME framework, we’ll need to add it as a dependency to our pom before we can actually use it:

首先,由于Spring的RSS支持是基于ROME框架的我们需要将其作为一个依赖项添加到我们的pom中,然后我们才能真正使用它。

<dependency>
    <groupId>com.rometools</groupId>
    <artifactId>rome</artifactId>
    <version>1.10.0</version>
</dependency>

For a guide to Rome have a look at our previous article.

关于罗马的指南,请看我们的以前的文章

4. Feed Implementation

4.饲料实施

Next up, we’re going to build the actual feed. In order to do that, we’ll extend the AbstractRssFeedView class and implement two of its methods.

接下来,我们要建立实际的feed。为了做到这一点,我们将扩展AbstractRssFeedView类并实现其中的两个方法。

The first one will receive a Channel object as input and will populate it with the feed’s metadata.

第一个将接收一个Channel对象作为输入,并将用feed的元数据填充它。

The other will return a list of items which represents the feed’s content:

另一个将返回一个代表feed内容的项目列表

@Component
public class RssFeedView extends AbstractRssFeedView {

    @Override
    protected void buildFeedMetadata(Map<String, Object> model, 
      Channel feed, HttpServletRequest request) {
        feed.setTitle("Baeldung RSS Feed");
        feed.setDescription("Learn how to program in Java");
        feed.setLink("http://www.baeldung.com");
    }

    @Override
    protected List<Item> buildFeedItems(Map<String, Object> model, 
      HttpServletRequest request, HttpServletResponse response) {
        Item entryOne = new Item();
        entryOne.setTitle("JUnit 5 @Test Annotation");
        entryOne.setAuthor("donatohan.rimenti@gmail.com");
        entryOne.setLink("http://www.baeldung.com/junit-5-test-annotation");
        entryOne.setPubDate(Date.from(Instant.parse("2017-12-19T00:00:00Z")));
        return Arrays.asList(entryOne);
    }
}

5. Exposing the Feed

5 暴露饲料

Finally, we’re going to build a simple REST service to make our feed available on the web. The service will return the view object that we just created:

最后,我们要建立一个简单的REST服务来使我们的feed在网络上可用。该服务将返回我们刚刚创建的视图对象。

@RestController
public class RssFeedController {

    @Autowired
    private RssFeedView view;
    
    @GetMapping("/rss")
    public View getFeed() {
        return view;
    }
}

Also, since we’re using Spring Boot to start up our application, we’ll implement a simple launcher class:

另外,由于我们使用Spring Boot来启动我们的应用程序,我们将实现一个简单的启动器类。

@SpringBootApplication
public class RssFeedApplication {
    public static void main(final String[] args) {
        SpringApplication.run(RssFeedApplication.class, args);
    }
}

After running our application, when performing a request to our service we’ll see the following RSS Feed:

运行我们的应用程序后,当对我们的服务执行请求时,我们会看到以下RSS提要。

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>Baeldung RSS Feed</title>
        <link>http://www.baeldung.com</link>
        <description>Learn how to program in Java</description>
        <item>
            <title>JUnit 5 @Test Annotation</title>
            <link>http://www.baeldung.com/junit-5-test-annotation</link>
            <pubDate>Tue, 19 Dec 2017 00:00:00 GMT</pubDate>
            <author>donatohan.rimenti@gmail.com</author>
        </item>
    </channel>
</rss>

6. Conclusion

6.结论

This article went through how to build a simple RSS feed with Spring and ROME and make it available for the consumers by using a Web Service.

本文介绍了如何用Spring和ROME建立一个简单的RSS源,并通过使用Web服务使其对消费者可用。

In our example, we used Spring Boot to start up our application. For more details on this topic, continue reading this introductory article on Spring Boot.

在我们的例子中,我们使用Spring Boot来启动我们的应用程序。有关这一主题的更多细节,请继续阅读这篇关于Spring Boot的介绍性文章

As always, all the code used is available over on GitHub.

一如既往,所有使用的代码都可以在GitHub上找到