RSS Feeds with Spring Boot

http://nixmash.com/post/rss-feeds-with-spring-boot

*******************************************************

We added a RSS Subscription to NixMash Spring Posts in like 10 minutes. Okay, probably longer than that but it was really quick.

Here's our feed XML, the Real Deal in RSS feeds. See it live here.


The FeedView

Creating RSS Feeds in Spring is all about its AbstractFeedView classes. There's an AbstractAtomFeedView, AbstractRssFeedView and other supporting classes.

In (1) you'll see we're extending Spring's AbstractRssFeedView class. Also notice on the first line we're explicitly naming the Component. This is important as we'll be passing it by name to the Controller. Next we create our Channel Header and populate it with ApplicationSettings from an external properties file (2). We retrieve our Post List, stream it, modify it with our createItem(post) method and return our list of Feed Items (3). Slick!

Spring uses the Rome Subscription Tools v1.5 or higher, so we need to add the RomeTools dependency in build.gradle.

compile 'com.rometools:rome:1.6.1'

The Controller

Now that we've created our Spring RssPostView all we have to do is add it to our model. Notice we're adding the Bean name, and Spring takes over in parsing and populating the feed from the Component. That's pretty cool.

Configuring the FeedView Resolver

You may see other posts that mention the need to configure the RssFeed View Resolver, since we're configuring a view resolver. Something like…

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.enableContentNegotiation(new RssPostFeedView());
}

…in a WebMvcConfigurer class. This is no longer needed, which is very wonderful.

The Test

Here's a quick test to confirm we're delivering media with content type application/rss-xml.

In Summary

To sum up, to create an RSS or Atom feed with Spring you

  1. Create a FeedView component that extends a Spring Abstract FeedView superclass
  2. Pass that component by name from your Controller method model to the client
Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted 七月 14, 2016 09:33 上午 EDT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值