2012-04-27 25 views
2

我使用Jersey作爲JSR 311的實現。我試圖轉換成JSON對象看起來是這樣的:澤西輸了id字段

@XmlRootElement 
public class deliveryTimes{ 
    private Integer id; 
    private Short type; 
    private Integer orderId; 
    /* ... more fields and autogenerated Getters & Setters ... */ 
} 

JSON結果:

{"deliveryTimes": 
[{"type":"1","orderId":"30449",/* ... other fields ... */ }, 
/* ... */ 
]} 

言之:字段「id」正在迷失。在我的其他對象中,id字段具有其他名稱,如orderId,customerId,...以及這些字段不會丟失。

我的pom.xml看起來是這樣的:

<!-- other stuff --> 
<!-- JERSEY --> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-server</artifactId> 
    <version>1.11</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-json</artifactId> 
    <version>1.11</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-grizzly2</artifactId> 
    <version>1.11</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-servlet</artifactId> 
    <version>1.11</version> 
</dependency> 

<!-- Jersey + Spring --> 
<dependency> 
    <groupId>com.sun.jersey.contribs</groupId> 
    <artifactId>jersey-spring</artifactId> 
    <version>1.11</version> 
    <exclusions> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
<!-- other stuff --> 

有沒有進一步的配置。 我沒有在球衣網站或通過谷歌找到任何有用的東西,所以我結束了在這裏與我的第一篇文章。

有沒有我缺少的配置選項?你如何JSONify ID字段?

回答

2

有幾個原因可能導致id未被編組。

1 - 它有一個空值

默認情況下,JAXB的實現不會名帥出空值。如果你想封出空值一定要添加下面的註釋

@XmlElement(nillable=true) 

詳細信息請參閱:

2 - 有一個領域,但不沒有訪問器(get/set)方法

默認情況下,JAXB只映射公共字段和accesso RS。任何不符合此標準的內容都將被視爲未映射。您可以通過指定XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class deliveryTimes{ 
    ... 
} 

或通過註釋字段

@XmlRootElement 
public class deliveryTimes{ 
    @XmlElement 
    private Integer id; 
} 

更多信息解決這個問題,請參見:

3 - 有一個get方法bu t沒有設置方法

如果有一個get方法但沒有伴隨set方法,那麼你的JAXB實現將它視爲一個未映射的屬性。要解決這個問題,你只需要將get方法映射到@XmlElement即可。

+0

感謝您的回答。其實我正在尋找一種方法來發出空值(你在#1中正確猜測)並將「id」的數據類型從int更改爲Integer,但忘記了setter。下次我會讓IDE重構它。再次感謝您的詳細有用答案(如果我有足夠的聲望,我會+ 1ed)! – 2012-05-07 07:00:21

0

「id」沒有什麼特別之處。從你班上的@XmlRootElement,我假設你正在使用JAXB進行編組(默認澤西島)。在默認配置中,JAXB需要一個getter將一個字段編組爲XML/JSON,另一個需要一個解析器來從XML/JSON解組字段。在這種情況下,我猜你缺少一個public Integer getId()方法。您可以通過將@XmlElement添加到您的id字段來輕鬆更改此行爲。

+1

或者,如果應該沒有屬性可以使用@XmlAccessorType(XmlAccessType.FIELD) – 2012-04-27 12:27:46