码迷,mamicode.com
首页 > 编程语言 > 详细

RSS Feeds with Spring Boot

时间:2017-06-02 09:46:33      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:9.png   like   androi   tps   subscript   type   needed   online   mvcc   

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 anAbstractAtomFeedView,AbstractRssFeedViewand other supporting classes.

In (1) you‘ll see we‘re extending Spring‘sAbstractRssFeedViewclass. 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 withApplicationSettingsfrom an external properties file (2). We retrieve our Post List, stream it, modify it with ourcreateItem(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 theRomeToolsdependency inbuild.gradle.

compile ‘com.rometools:rome:1.6.1‘

The Controller

Now that we‘ve created our SpringRssPostViewall 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 aWebMvcConfigurerclass. This is no longer needed, which is very wonderful.

The Test

Here‘s a quick test to confirm we‘re delivering media with content typeapplication/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
 

RSS Feeds with Spring Boot

标签:9.png   like   androi   tps   subscript   type   needed   online   mvcc   

原文地址:http://www.cnblogs.com/zhao1949/p/6931629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!