java开发自己站点的RSS服务

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 10:58   11   0

查找资料,用到了开源技术 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!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP