2012-04-27 2 views
2

JSR 311 구현으로 Jersey를 사용하고 있습니다. 내가 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 --> 

추가 구성이 없습니다. 저는 저지 웹 사이트 나 Google을 통해 도움이되는 정보를 찾지 못했습니다. 그래서 나는 처음으로 내 첫 번째 게시물을 남겼습니다.

누락 된 구성 옵션이 있습니까? ID 필드를 JSON 화하는 방법은 무엇입니까?

답변

2

id을 마샬링 할 수없는 데는 여러 가지 이유가 있습니다.

1 - 그것은 null 값을 마샬링하지 않습니다 기본 JAXB 구현으로 null 값

있습니다. 이 필드입니다하지만 -

2 : 당신이 밖으로 마샬링하려면 null 값은 자세한 정보는 다음의 주석

@XmlElement(nillable=true) 

을 추가해야합니다 접근 자 (get/set) 메서드 없음

기본적으로 JAXB는 공개 필드 및 액세스 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

당신의 답변에 감사드립니다. 실제로 null 값 (# 1에서 정확하게 추측)을 전달하고 "id"의 데이터 유형을 int에서 Integer로 변경했지만 setter를 잊어 버렸습니다. 다음에 나는 IDE가 그것을 리팩터링하도록 할 것이다. 당신의 상세한 도움이되는 답변을 한 번 더 주셔서 감사합니다 (충분한 평판을 얻었 으면 좋겠어요). –

0

"id"에 특별한 것은 없습니다. 클래스의 @XmlRootElement에서 저는 마샬링을 위해 JAXB를 사용하고 있다고 가정합니다 (Jersey의 기본값). 기본 구성에서 JAXB는 필드를 XML/JSON으로 마샬링하고 setter를 사용하여 XML/JSON에서 필드를 비 정렬 화해야합니다. 이 경우 public Integer getId() 메소드가 부족한 것 같습니다. id 입력란에 @XmlElement을 추가하여이 동작을 쉽게 변경할 수 있습니다.

+1

또는 속성이 없으면 @XmlAccessorType (XmlAccessType.FIELD)을 사용할 수 있습니다. –

관련 문제