RESTEasy json JAXB 解组问题

发布于 2024-11-17 11:01:19 字数 4348 浏览 2 评论 0原文

我在 Play 中编写类似小型高音扬声器的应用程序!使用 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"
         }
      ]
   }
}

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.printf("%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"
         }
      ]
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅哥哥的热头脑 2024-11-24 11:01:19

尝试像这样格式化您的 json 请求:

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

这是我必须为所有 json 请求执行的操作。我不知道为什么它会以这种方式工作,而不是以其他方式工作。

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.

慕巷 2024-11-24 11:01:19

这可能取决于您使用的 json 提供程序,但首先尝试将 @XmlElement(name = "tweet") 从 setter 移动到客户端包装器中的 getter (getTweets) 方法。其他一切看起来都很正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文