java开发自己站点的RSS服务
程序员文章站
2022-05-13 12:53:03
...
查找资料,用到了开源技术 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!
推荐阅读
-
Java开发之使用websocket实现web客户端与服务器之间的实时通讯
-
Android聊天室开发(java写的后端服务)
-
适用于Java开发人员的微服务:性能和负载测试
-
系统设计和开发中,方法论比技术更重要--兼谈怎样做Java服务器的性能分析和调整
-
开发一款app,php做服务端,有一个功能是附近的人和发布动态的时候发布自己的定位,php世界有啥好的方案去做这些吗?
-
从网上找了个php开发的zlchat视频会议,自己局域网能用,放到服务器上就不能用,是怎么回事
-
【流水账】利用闲置笔记本搭建自己的开发服务器
-
Java开发之使用websocket实现web客户端与服务器之间的实时通讯
-
Android聊天室开发(java写的后端服务)
-
java开发自己站点的RSS服务