WEB中使用ROME实现RSS服务
WEB中使用ROME实现RSS服务
首页
http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialFeedServlet
文件
rome-1.0RC1.jar
1、配置RSS服务
其实挺简单,没啥好配置的,我是配置在我的springmvccontroller中使用的
先在pom.xml中导入这个东东,先导入到maven2私服,然后修改pom.xml
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
嘿嘿,修改springside上的BaseManageController基类(我用的springside2上的基类,感觉还挺清晰的)
增加了如下方法
/**
*RSS支持
*@paramrequest
*@paramresponse
*@return
*@throwsException
*/
publicModelAndViewrss(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
onRss(request,response);
returnnull;
}
//等着被overwrite
protectedvoidonRss(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
}
在基类BaseController中加入如下代码:
privatestaticfinalStringMIME_TYPE="application/xml;charset=UTF-8";
//rss_0.90,rss_0.91,rss_0.92,rss_0.93,rss_0.94,rss_1.0,rss_2.0,
//atom_0.3
privatestaticfinalStringRSS_TYPE="rss_2.0";
//我直接写死的rss_2.0,其实也没有啥好配置的
/**
*直接向客户端返回,不用通过View页面渲染.
*/
protectedvoidrendText(HttpServletResponseresponse,SyndFeedfeed)
throwsIOException{
response.setCharacterEncoding("UTF-8");
feed.setFeedType(RSS_TYPE);
response.setContentType(MIME_TYPE);
SyndFeedOutputoutput=newSyndFeedOutput();
try{
output.output(feed,response.getWriter());
}catch(IOExceptione){
e.printStackTrace();
}catch(FeedExceptione){
e.printStackTrace();
}
}
找了个UserContoller.java来演示一下,当然实际中,应该是从manager层得到数据吧,这里我是MOCK一些数据来DEMO的
protectedvoidonRss(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
SyndFeedfeed=newSyndFeedImpl();
feed.setTitle("Channeltitle");
feed.setLink("http://hi.baidu.com/luohuazju");
feed.setDescription("Channeltitle,javademo");
Listentries=newArrayList();
SyndEntryentry;
SyndContentdescription;
entry=newSyndEntryImpl();
entry.setTitle("RSSdemo");
entry.setLink("http://hi.baidu.com/luohuazju");
entry.setPublishedDate(newDate());
description=newSyndContentImpl();
description.setType("text/plain");
description.setValue("justtheguideintheromesite");
entry.setDescription(description);
entries.add(entry);
feed.setEntries(entries);
rendText(response,feed);
}
服务配置就结束了,弄个reader来读取下看。
2、reader读取RSS
核心代码如下:
URLfeedUrl=null;
try{
feedUrl=newURL("http://www.kiko.com/search/user.do?method=rss");
}catch(MalformedURLExceptione){
e.printStackTrace();
}
SyndFeedInputinput=newSyndFeedInput();
try{
SyndFeedfeed=input.build(newXmlReader(feedUrl));
System.out.println(feed.getTitle());
Listlist=feed.getEntries();
for(inti=0;i<list.size();i++){
SyndEntryentry=(SyndEntry)list.get(i);
System.out.println(entry.getTitle()+":"
+entry.getPublishedDate());
System.out.println(entry.getLink());
}
}catch(IllegalArgumentExceptione){
e.printStackTrace();
}catch(FeedExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
就上面的server和reader的列子,读出的结果是:
Channeltitle
RSSdemo:SatSep2717:06:11CST2008
http://hi.baidu.com/luohuazju