开发者

RESTEasy json JAXB unmarshalling issue

I writing small Tweeter like application in Play! with simple REST API using RESTEasy. I have a simple resource:

@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);
}

I also have this little wrapper class:

    @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;
        }
    }

On the client side, I have similar wrapper class and a Client side interface for RESTEasy proxy creation, it looks like this:

@GET
@Path("/tweets/all")
@Produces("application/xml")
public TweetList all(@QueryParam("page") @DefaultValue("1") Integer page);

My code for getting the data:

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.pr开发者_如何学JAVAintf("%s wrote %s, %s \n", t.author.fullName, t.content, t.id);
        }

And this works perfectly for XML - all tweets are printed on the screen as they should be. The problem is that I'd like to use JSON as return format. When I change my resource and proxy @Produces annotation to @Produces("application/json"), I'm getting this error message:

Unrecognized field "tweetList" (Class rest.client.wrappers.TweetList), not marked as ignorable

My client side wrapper:

@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;
    }
}

And I don't know how to bypass this issue. Sample output from calling my resource by hand in browser:

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"
         }
      ]
   }
}


Try formatting your json request like so:

{
  "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"
     }
  ]
}

This is what I've had to do for all my json requests. I don't know why it works this way and not the other way.


It could depend on which json provider are you using, but first try moving the @XmlElement(name = "tweet") from the setter to the getter (getTweets) method in the client side wrapper. Everything else looks normal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜