Trifork Blog

Implementing RSS Feeds with new features of Spring 3

May 5th, 2010 by

In this post I explain how we implemented the way we created the RSS feeds in a project and the challenges that we had during the set-up.

My colleague Jettro Coenradie explained in a previous post how you can create a feed using Rome and Spring 3, but didn’t elaborated on the Spring 3 part. I will explain how we used Spring 3 to create the feeds.


In this project we need to support a large number of different feeds. As an example I will use the news feed that we need to create for different subjects. The subjects are created in a CMS and therefore people can add subjects whenever they want. This means that we want to support dynamically created feeds.


The controller is the place where the request mapping takes place.

public class SubjectRssController extends AbstractRssController {

    @RequestMapping(value = "/subjects/{subjectName}/news.rss", method = RequestMethod.GET)
    public String generateSubjectNewsFeedItems(ModelMap modelMap, @PathVariable final String subjectName,
                                               HttpServletRequest request, RssSubjectDao subjectDao) throws ServletException {
        SubjectBean subject = findSubject(subjectName, request, subjectDao);

        RssQueryResultMapper mapper = getSubjectQueryResultMapper();
        List<item> items = subjectDao.findNewsForSubject(subject, mapper);

        modelMap.addAttribute("items", items);
        modelMap.addAttribute("subjectName", subject.getTitle());
        return "subjectRssFeed";


The first thing you might notice when you look at the code above are the two annotations (@SessionManagedDaos and @RequestMapping) on the method. The @SessionManagedDaos is an annotation that is created by us. This annotation manages the session that we use from the CMS connection pool.

The second annotation is the @RequestMapping. This annotation has the value “/subjects/{subjectName}/news.rss” which means that every path that matches this pattern will be mapped to this method. In this pattern uses a new feature of Spring 3, namely the variable in the path. The @PathVariable annotation in front of the parameter subjectName indicates that the variable in the path needs to be bound to the method parameter.

This helps us to support any request with a given subject name and therefore we do not need to change the code if a new subject is created.

Wrong request

With the freedom of the path pattern also comes the problem that a person can try any subject name that he wants. In order to prevent that we execute a query for a subject that does not exist, we execute a query that finds the subject. If the subject is not found, we throw a NoSuchRequestHandlingMethodException. This exception is handled by the DefaultHandlerExceptionResolver. This resolver handles certain standard Spring MVC exceptions by setting a specific response status code. In our case the status code will be a 404.

protected SubjectBean findSubject(String subjectName, HttpServletRequest request, RssSubjectDao subjectDao)
                                  throws NoSuchRequestHandlingMethodException {
    SubjectBean subject = subjectDao.findSubjectBeanByName(subjectName);
    if (subject == null) {
        throw new NoSuchRequestHandlingMethodException(request);

    return subject;


The implementation of the controller is not special. We execute a query that finds some news and with the help of a mapper, we map the retrieved beans to RSS items. These items, as well as the subject title, are added to the model.


Now that our model is filled, we can create the view. Spring 3 provides us with the AbstractRssFeedView. This view has a few entry points to create the feed.

We created a BaseRssFeedView which extends the AbstractRssFeedView. The BaseRssFeedView overrides three methods that build the feed.

public abstract class BaseRssFeedView extends AbstractRssFeedView {

    private LocalizedMessageResolver messages;

    protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
        super.prepareResponse(request, response);

    protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) {
        RssMetaData rssMetaData = new RssMetaData();
        configureRssMetaData(rssMetaData, model);


        Image image = resolveImage(rssMetaData.getFeedTitle());
        feed.setLastBuildDate(new Date());

        AtomNSModule module = new AtomNSModuleImpl();

    protected List<Item> buildFeedItems(Map<String, Object> stringObjectMap, HttpServletRequest request, HttpServletResponse response) {
        return (List<Item>) stringObjectMap.get("items");

    protected abstract void configureRssMetaData(RssMetaData metaData, Map<String, Object> viewModel);

I don’t want to discuss the implementation of the methods, as I think that they are pretty self-explanatory. But I do want to discuss the one abstract method that needs to be implemented by the subclasses. This method allows the subclass to specify the specific metadata for the feed. The code below shows the view that would belong to our SubjectRssController.

public class SubjectRssFeedView extends BaseRssFeedView {

    protected void configureRssMetaData(RssMetaData metaData, Map<String, Object> viewModel) {
        String subjectName = (String) viewModel.get("subjectName");

        LocalizedMessageResolver messages = getMessageResolver();
        metaData.setFeedUrl(messages.getMessage("", subjectName));
        metaData.setFeedTitle(messages.getMessage("", subjectName));
        metaData.setFeedDescription(messages.getMessage("", subjectName));

The class is annotated with @Component(“subjectRssFeed”). The name of the view is subjectRssFeed and needs to match with the view name you return in the controller.


In this case we assume that every subject has the same message for the specific metadata. So the only thing that differs is the subject name. For example, the description of a feed of the subject cars could be: This feed contains the news items of the subject cars.

So this means that we need to parameterize the messages. In order to do that we have created a class that resolves the message for us from the correct property file. This way we can still use the templates in the property files. feed contains the news items of the subject {0}

Spring 3 also lets you annotate a field or method/constructor parameter that indicates a default value expression for the affected argument. This means you could inject a property like this:

public void setFeedUrl(String feedUrl) {
    this.feedUrl = feedUrl;


private String feedUrl;


I hope you got an idea of how Spring 3 can help you with creating RSS feeds. In our case it was really useful as we get all the advantages of Spring MVC and added new feeds are a piece of cake!

Comments are closed.