java开发自己站点的RSS服务

阅读更多

查找资料,用到了开源技术 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 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-1_0RC2.jar (19.1 KB)
  • 下载次数: 3
  • EXML.zip (70 KB)
  • 下载次数: 4

你可能感兴趣的:(Rss,rsslibj)