Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用REST服务返回RSS?_Java_Rest_Rss_Jersey_Rome - Fatal编程技术网

Java 如何使用REST服务返回RSS?

Java 如何使用REST服务返回RSS?,java,rest,rss,jersey,rome,Java,Rest,Rss,Jersey,Rome,我正在使用RSS提要生成和Jersey作为REST服务 这是我的RSS提要生成 public SyndFeed generate() { SyndFeed feed = new SyndFeedImpl(); feed.setFeedType( "rss_2.0" ); feed.setTitle( "My Site" ); feed.setLink("http://example.com"); feed.setDescription("Test Site

我正在使用RSS提要生成和Jersey作为REST服务

这是我的RSS提要生成

public SyndFeed generate()
{
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType( "rss_2.0" );
    feed.setTitle( "My Site" );
    feed.setLink("http://example.com");
    feed.setDescription("Test Site.");

    List<SyndEntry> entries = new ArrayList<SyndEntry>();
    SyndEntry entry = null;
    SyndContent description = null;

    entry = new SyndEntryImpl();
    entry.setTitle( "Entry1" );
    entry.setLink( "http://example.com/entry1" );
    entry.setPublishedDate( new Date() );

    description = new SyndContentImpl();
    description.setType("text/html");
    description.setValue( "This is the content of entry 1." );
    entry.setDescription( description );

    entries.add( entry );
    feed.setEntries(entries);

    return feed;
}

我发现body writer的不兼容类型出错。如何使服务返回带有提要的XML?

Jersey不知道如何将
SyndFeed
的实例映射到XML。这很有效

@Path(“堆栈溢出”)
公共类资源{
@得到
@路径(“/feed”)
@生成(“应用程序/rss+xml”)
公共响应getFeed()引发IOException,FeedException{
最终SyndFeed feed=generate();
//将SyndFeed写入编写器。
最终SyndFeedOutput输出=新的SyndFeedOutput();
最终编写器=新的StringWriter();
输出(feed,writer);
//返回以编写器为主体的JAX-RS响应。
返回Response.ok(writer.toString()).build();
}
专用SyndFeed generate(){
最终SyndFeed提要=新SyndFeedImpl();
feed.setFeedType(“rss_2.0”);
feed.setTitle(“我的网站”);
feed.setLink(“http://example.com");
feed.setDescription(“测试站点”);
最终列表条目=新的ArrayList();
最终SyndEntry条目=新SyndEntryImpl();
条目。设置标题(“条目1”);
entry.setLink(“http://example.com/entry1");
entry.setPublishedDate(新日期());
最终合成内容描述=新合成内容描述();
description.setType(“text/html”);
description.setValue(“这是条目1的内容”);
条目.setDescription(描述);
条目。添加(条目);
feed.setEntries(条目);
回馈;
}
}
获取
资源:

$ curl -v http://localhost:8080/StackOverflowWeb/webresources/stackoverflow/feed
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /StackOverflowWeb/webresources/stackoverflow/feed HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.42.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: GlassFish Server Open Source Edition  4.1
< X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1  Java/Oracle Corporation/1.8)
< Content-Type: application/rss+xml
< Date: Sun, 14 Jun 2015 13:15:54 GMT
< Content-Length: 565
<
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>My Site</title>
    <link>http://example.com</link>
    <description>Test Site.</description>
    <item>
      <title>Entry1</title>
      <link>http://example.com/entry1</link>
      <description>This is the content of entry 1.</description>
      <pubDate>Sun, 14 Jun 2015 13:15:54 GMT</pubDate>
      <guid>http://example.com/entry1</guid>
      <dc:date>2015-06-14T13:15:54Z</dc:date>
    </item>
  </channel>
</rss>

* Connection #0 to host localhost left intact
$curl-vhttp://localhost:8080/StackOverflowWeb/webresources/stackoverflow/feed
*正在尝试::1。。。
*已连接到本地主机(::1)端口8080(#0)
>GET/StackOverflowWeb/webresources/stackoverflow/feed HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.42.1
>接受:*/*
>
$ curl -v http://localhost:8080/StackOverflowWeb/webresources/stackoverflow/feed
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /StackOverflowWeb/webresources/stackoverflow/feed HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.42.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: GlassFish Server Open Source Edition  4.1
< X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1  Java/Oracle Corporation/1.8)
< Content-Type: application/rss+xml
< Date: Sun, 14 Jun 2015 13:15:54 GMT
< Content-Length: 565
<
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>My Site</title>
    <link>http://example.com</link>
    <description>Test Site.</description>
    <item>
      <title>Entry1</title>
      <link>http://example.com/entry1</link>
      <description>This is the content of entry 1.</description>
      <pubDate>Sun, 14 Jun 2015 13:15:54 GMT</pubDate>
      <guid>http://example.com/entry1</guid>
      <dc:date>2015-06-14T13:15:54Z</dc:date>
    </item>
  </channel>
</rss>

* Connection #0 to host localhost left intact