查找资料,用到了开源技术 rsslibj。
public class RssController {
@Autowired
Service service;
static final String domain = "http://www.you.com";
static final String info = "/news/info/";
/**
* 创建一个自定义的RSS的
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
@RequestMapping(value="")
@ResponseBody
public String writerRSS(HttpServletResponse response) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
Channel channel=new Channel();
channel.setDescription("网站站");
channel.setLink(domain);
channel.setTitle("网站名称");
channel.setImage("http://www.you.com/images/logo.png",
"LOGO Image",
"网站名称");
channel.addItem(domain+"/theme/1", "专题1","专题1");
此处即你需要在rss服务中的频道项目
List<News> newsList= service.findNewsList();
for (News news: newsList) {
channel.addItem(domain+info+new.getId(), "新闻","新闻");
}
response.setContentType("application/xml");
PrintWriter pw;
try {
pw = response.getWriter();
pw.print(channel.getFeed("rss"));
pw.flush();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
至此 ,访问就可以访问该服务啦。
期间遇到了“java.lang.IllegalStateException: writer”异常,其实就是少了pw.flush();加上之后一切OK!
本文介绍了一个基于rsslibj开源技术的RSS服务实现过程,详细展示了如何通过Java代码创建RSS源并将其发布出去的方法。文中还分享了作者在开发过程中遇到的问题及解决方式。
372

被折叠的 条评论
为什么被折叠?



