2011-06-29 69 views
1

我在Play中編寫小型高音揚聲器應用程序!用簡單的REST API使用RESTEasy。我有一個簡單的資源:RESTEasy json JAXB反編組問題

@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來說非常適用 - 所有的tweets都應該在屏幕上打印。問題是我想使用JSON作爲返回格式。當我改變我的資源和代理@Produces註解@Produces( 「應用/ 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" 
     } 
     ] 
    } 
} 

回答

0

這可能取決於哪一個JSON提供您使用的,但第一次嘗試移動@XmlElement(name =「tweet」)從setter到客戶端封裝器中的getter(getTweets)方法。一切看起來很正常。

+0

我按照你的建議移動了這個註解,但沒有任何改變。從拋出的異常中我可以看出,我看到傑克遜被使用了。 – jjczopek

1

嘗試格式化你的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請求做什麼。我不知道它爲什麼會這樣,而不是其他方式。