Spring MVC - Atom Feed View

[Updated: Oct 9, 2017, Created: Oct 9, 2017]

This example shows how to use ROME Library to generate Atom feed in a Spring MVC application.

Example

Additional Maven Dependency

pom.xml

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

Java Config class

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
  @Bean
  public ViewResolver beanNameViewResolver() {
      BeanNameViewResolver resolver = new BeanNameViewResolver();
      return resolver;
  }

  @Bean
  public ViewResolver jspViewResolver() {
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setPrefix("/WEB-INF/views/");
      viewResolver.setSuffix(".jsp");
      return viewResolver;
  }
}

In above configuration, we are using BeanNameViewResolver for resolving the Atom view by it's bean name.

Implementing the View

Spring provides AbstractRssFeedView support class for generating ROME's RSS feed.

package com.logicbig.example;

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 org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.feed.AbstractAtomFeedView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component("atomFeedView")
public class MyAtomFeedView extends AbstractAtomFeedView {

  @Override
  protected void buildFeedMetadata(Map<String, Object> model, Feed feed,
                                   HttpServletRequest request) {

      FeedInfo feedInfo = (FeedInfo) model.get("feedInfo");
      feed.setTitle(feedInfo.getTitle());
  }


  @Override
  protected List<Entry> buildFeedEntries(Map<String, Object> model,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
      List<Entry> entries = new ArrayList<>();
      FeedInfo feedInfo = (FeedInfo) model.get("feedInfo");
      for (FeedEntry fe : feedInfo.getFeedEntries()) {
          Entry entry = new Entry();
          entry.setTitle(fe.getTitle());

          Link link = new Link();
          link.setHref(fe.getLink());
          entry.setAlternateLinks(Arrays.asList(link));

          entry.setPublished(fe.getDate());

          Content content = new Content();
          content.setValue(fe.getContent());
          entry.setSummary(content);
          entries.add(entry);
      }

      return entries;
  }
}
public class FeedInfo {
  private String title;
  private String link;
  private String desc;
  private List<FeedEntry> feedEntries = new ArrayList<>();
    .............
}
public class FeedEntry {
  String title;
  String link;
  String content;
  Date date;
    .............
}

A Spring Controller

@Controller
@RequestMapping("/")
public class AtomFeedController {

  @GetMapping("/")
  public String mainView() {
      return "main";
  }

  @GetMapping("/atomFeed")
  public String handleForexRequest(Model model) {
      model.addAttribute("feedInfo", getFeedInfo());
      return "atomFeedView";
  }

  private static FeedInfo getFeedInfo() {
      FeedInfo feedInfo = new FeedInfo();
      feedInfo.setTitle("Logicbig Tutorials");
      feedInfo.setLink("http://www.logicbig.com");
      feedInfo.setDesc("Logicbig tutorials feed");

      List<FeedEntry> list = new ArrayList<>();
      list.add(createFeedEntry("Spring Framework", "spring-framework",
              "Spring Framework Tutorials", new Date()));
      list.add(createFeedEntry("Java EE", "java-ee-tutorial",
              "Java EE Tutorials", new Date()));
      list.add(createFeedEntry("Java Core", "/core-java-tutorial/",
              "Core Java Tutorials", new Date()));
      feedInfo.setFeedEntries(list);
      return feedInfo;
  }

  private static FeedEntry createFeedEntry(String title, String uri,
                                           String content, Date date) {
      FeedEntry fe = new FeedEntry();
      fe.setTitle(title);
      fe.setLink("http://www.logicbig.com/tutorials/" + uri);
      fe.setContent(content);
      fe.setDate(date);
      return fe;
  }
}

src/main/webapp/WEB-INF/views/main.jsp

<html>
<body>
<h2>Spring Atom Feed Example</h2>
<a href="/atomFeed">Tutorials Feed</a>
</body>
</html>

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

On clicking the 'Tutorials Feed' link:

Example Project

Dependencies and Technologies Used :

  • spring-webmvc 5.0.0.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.1.0 Java Servlet API
  • rome 1.8.0: All Roads Lead to ROME. ROME is a set of Atom/RSS Java utilities that make it easy to work in Java with most syndication formats. Today it accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0), Atom 0.3 and Atom 1.0 feeds. Rome includes a set of parsers and generators for the various flavors of feeds, as well as converters to convert from one format to another. The parsers can give you back Java objects that are either specific for the format you want to work with, or a generic normalized SyndFeed object that lets you work on with the data without bothering about the underlying format. [Description from rome-1.8.0.pom]
  • JDK 1.8
  • Maven 3.3.9

Spring Atom Feed View Example Select All Download
  • atom-feed-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • WEB-INF
            • views

See Also