Create your own RSS feed with Spring and ROME in 5 minutes
Therefore most of these sites provide their content as RSS (an abbreviation for Rich Site Summary) feed. RSS is a technology providing information in a standardized (XML) way. There are few formats in various versions specified, but the most common and current ones are ‘RSS 2.0’ and ‘Atom 1.0’.
Most modern web browsers can read RSS feeds. Additionally there are hundreds of apps for each operating system and flavor of user.
This blog post demonstrates how to provide your own RSS Atom 1.0 feed with Spring Boot, Spring REST and the ROME library.
The open source ROME library is released under the terms of the Apache 2.0 license. It provides functionality to read, write and convert data between XML (feed) and human readable contents.
Before starting please make sure that you have installed a current Java JDK and an IDE (if desired). Now let’s start implementing the RSS feed.
1. Generate Spring Boot project
Navigate with your web browser to start.spring.io and generate a Gradle Project using Spring Boot 1.4.0 and the “Web” dependency.
Extract the zip archive and import it to your IDE.
2. Add ROME library dependency to gradle build file
Open the build.gradle file in the root directory of the extracted folder. Add the ROME dependency (compile group: ‘com.rometools’, name: ‘rome’, version: ‘1.7.0’) above the existing ones:
1 2 3 4 5 |
dependencies { compile('com.rometools:rome:1.7.0') compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') } |
3. Create REST controller
Open the file demo/src/main/java/com/example/DemoApplication.java and modify the source code as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.example; import java.util.Collections; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.rometools.rome.feed.synd.SyndContent; import com.rometools.rome.feed.synd.SyndContentImpl; import com.rometools.rome.feed.synd.SyndEntry; import com.rometools.rome.feed.synd.SyndEntryImpl; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.feed.synd.SyndFeedImpl; import com.rometools.rome.io.FeedException; import com.rometools.rome.io.SyndFeedOutput; @RestController @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/") @ResponseBody public String getFeed () throws FeedException { SyndFeed feed = new SyndFeedImpl(); feed.setFeedType("atom_1.0"); SyndEntry entry = new SyndEntryImpl(); entry.setTitle("Sample feed title"); SyndContent content = new SyndContentImpl(); content.setValue("Sample feed content"); entry.setContents(Collections.singletonList(content)); feed.setEntries(Collections.singletonList(entry)); return new SyndFeedOutput().outputString(feed); } } |
The following table explains what’s happening in the previously shown class line by line.
Line | Meaning |
20 | Register class as REST controller in the spring context |
21 | Mark application as Spring Boot application so that it’s auto configured at startup |
25 | Main method to bootstrap the application. Starts the tomcat server and registers the current REST controller to it. |
28 | Register the following method to be called when the root resource is requested (e.g. http://localhost:8080/) |
29 | Define that the response is plain text |
31 | Create RSS feed |
32 | Set feed type to Atom Version 1.0 |
34-38 | Create (news) feed entry, set title, create content and set text to publish |
40-41 | Add content to entry and entry to feed |
43 | Export feed as String and return it as REST call response |
4. Build and run
Finally build the Gradle application on the command line using the command:
./gradlew clean build
Navigate on the command line to the directory build/libs and execute the following command to start the application:
java -jar demo-0.0.1-SNAPSHOT.jar
Open your favorite RSS reader or web browser and navigate to http://<hostname>:8080/. As you can see there is the RSS feed.
That’s it!
You’ve created your first simple RSS feed using Spring Boot, Spring REST and the ROME library. If you’re looking for some future inspiration how to enrich your feed, please have a look on the list of tutorials and articels.
Recent posts






Comment article