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