我在 Play 中编写类似应用程序的小型 Tweeter!通过使用 RESTEasy 的简单 REST API。我有一个简单的资源:
@GET
@Path("/tweets/all")
@Produces("application/xml")
public TweetList all(@QueryParam("page") @DefaultValue("1") Integer page) {
//return Tweet.find("order by dateCreated desc").fetch(page, 100);
List<Tweet> l = Tweet.find("order by dateCreated desc").fetch(page, 100);
return new TweetList(l);
}
我也有这个小包装类:
@XmlRootElement(name = "tweetList")
private class TweetList {
private List<Tweet> tweets;
public TweetList(List<Tweet> tweets) {
this.tweets = tweets;
}
public TweetList() {
}
@XmlElement(name = "tweet")
public List<Tweet> getTweets() {
return tweets;
}
public void setTweets(List<Tweet> tweets) {
this.tweets = tweets;
}
}
在客户端,我有类似的包装类和用于创建 RESTEasy 代理的客户端接口,如下所示:
@GET
@Path("/tweets/all")
@Produces("application/xml")
public TweetList all(@QueryParam("page") @DefaultValue("1") Integer page);
我获取数据的代码:
Tweet tweet = ProxyFactory.create(Tweet.class, "http://localhost:9000/rest");
TweetList tweetList = tweet.all(null);
for(rest.client.beans.Tweet t : tweetList.getTweets()){
System.out.printf("%s wrote %s, %s \n", t.author.fullName, t.content, t.id);
}
这非常适用于 XML - 所有推文都按应有的方式打印在屏幕上。问题是我想使用 JSON 作为返回格式。当我将资源和代理 @Produces 注释更改为 @Produces("application/json") 时,我收到以下错误消息:
Unrecognized field "tweetList" (Class rest.client.wrappers.TweetList), not marked as ignorable
我的客户端包装器:
@XmlRootElement(name = "tweetList")
public class TweetList {
private List<Tweet> tweets;
public TweetList(List<Tweet> tweets) {
this.tweets = tweets;
}
public TweetList() {
}
public List<Tweet> getTweets() {
return tweets;
}
@XmlElement(name = "tweet")
public void setTweets(List<Tweet> tweets) {
this.tweets = tweets;
}
}
而且我不知道如何绕过这个问题。在浏览器中手动调用我的资源的示例输出:
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tweetList>
<tweet>
<id>129</id>
<content>aksjdlkajsdlkjaskjdajdlakjsdljasdlkjakdjaljdlkajsd kajsdlkajsdl kajsdlkajsdl asdlkja lkdsjalksd</content>
<author>
<id>1</id>
<login>admin</login>
<fullName>Administrator</fullName>
</author>
<dateCreated>2011-06-13T21:08:03.145+02:00</dateCreated>
</tweet>
<tweet>
<id>98</id>
<content>Donec pulvinar porta feugiat. Sed adipiscing eros at libero mollis commodo. Duis auctor, tortor ac ultricies facilisis, purus velit fermentum elit, id luctus diam enim et felis.</content>
<author>
<id>2</id>
<login>user1</login>
<fullName>UserOne</fullName>
</author>
<dateCreated>2011-04-30T02:00:00+02:00</dateCreated>
</tweet>
</tweetList>
JSON:
{
"tweetList":{
"tweet":[
{
"id":129,
"content":"aksjdlkajsdlkjaskjdajdlakjsdljasdlkjakdjaljdlkajsd kajsdlkajsdl kajsdlkajsdl asdlkja lkdsjalksd",
"author":{
"id":1,
"login":"admin",
"fullName":"Administrator"
},
"dateCreated":"2011-06-13T21:08:03.145+02:00"
},
{
"id":98,
"content":"Donec pulvinar porta feugiat. Sed adipiscing eros at libero mollis commodo. Duis auctor, tortor ac ultricies facilisis, purus velit fermentum elit, id luctus diam enim et felis.",
"author":{
"id":2,
"login":"user1",
"fullName":"UserOne"
},
"dateCreated":"2011-04-30T02:00:00+02:00"
}
]
}
}