如何使用 REST 服务 return RSS?
How to return RSS with REST service?
我正在使用 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.");
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;
}
以及获取提要的方法
@GET
@Path("/getFeed")
@Produces(MediaType.APPLICATION_XML)
public SyndFeed getFeed()
{
RSSFeed rssFeed = new RSSFeed ();
return rssFeed.generate();
}
我收到正文编写器类型不兼容的错误。如何使用 Feed 为 return XML 提供服务?
Jersey 不知道如何将 SyndFeed
的实例映射到 XML。这行得通。
@Path("Whosebug")
public class RomeRessource {
@GET
@Path("/feed")
@Produces("application/rss+xml")
public Response getFeed() throws IOException, FeedException {
final SyndFeed feed = generate();
// Write the SyndFeed to a Writer.
final SyndFeedOutput output = new SyndFeedOutput();
final Writer writer = new StringWriter();
output.output(feed, writer);
// Return a JAX-RS Response with the Writer as the body.
return Response.ok(writer.toString()).build();
}
private SyndFeed generate() {
final SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("My Site");
feed.setLink("http://example.com");
feed.setDescription("Test Site.");
final List<SyndEntry> entries = new ArrayList<>();
final SyndEntry entry = new SyndEntryImpl();
entry.setTitle("Entry1");
entry.setLink("http://example.com/entry1");
entry.setPublishedDate(new Date());
final SyndContent 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;
}
}
GET
资源:
$ curl -v http://localhost:8080/WhosebugWeb/webresources/Whosebug/feed
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /WhosebugWeb/webresources/Whosebug/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
我正在使用 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.");
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;
}
以及获取提要的方法
@GET
@Path("/getFeed")
@Produces(MediaType.APPLICATION_XML)
public SyndFeed getFeed()
{
RSSFeed rssFeed = new RSSFeed ();
return rssFeed.generate();
}
我收到正文编写器类型不兼容的错误。如何使用 Feed 为 return XML 提供服务?
Jersey 不知道如何将 SyndFeed
的实例映射到 XML。这行得通。
@Path("Whosebug")
public class RomeRessource {
@GET
@Path("/feed")
@Produces("application/rss+xml")
public Response getFeed() throws IOException, FeedException {
final SyndFeed feed = generate();
// Write the SyndFeed to a Writer.
final SyndFeedOutput output = new SyndFeedOutput();
final Writer writer = new StringWriter();
output.output(feed, writer);
// Return a JAX-RS Response with the Writer as the body.
return Response.ok(writer.toString()).build();
}
private SyndFeed generate() {
final SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("My Site");
feed.setLink("http://example.com");
feed.setDescription("Test Site.");
final List<SyndEntry> entries = new ArrayList<>();
final SyndEntry entry = new SyndEntryImpl();
entry.setTitle("Entry1");
entry.setLink("http://example.com/entry1");
entry.setPublishedDate(new Date());
final SyndContent 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;
}
}
GET
资源:
$ curl -v http://localhost:8080/WhosebugWeb/webresources/Whosebug/feed
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /WhosebugWeb/webresources/Whosebug/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