I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Introduction

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

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

2. RSS Feed

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

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.

3. Dependencies

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:

<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

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.

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

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

@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("[email protected]");
        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

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:

@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:

@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:

<?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>[email protected]</author>
        </item>
    </channel>
</rss>

6. Conclusion

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.

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.

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

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS