2017-02-15 3 views
0

아래와 같이 주어진 클래스에 대해 직렬화 된 JSON을 얻지 못하는 이유를 모르겠습니다.FasterXML Jackson을 사용한 이상한 JSON 직렬화

이 WSDL로부터 생성 된 클래스, 그래서 그것을 변경할 수 없습니다 :

클래스 인스턴스가 fasterxml.jackson 직렬화됩니다
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "Lawyer") 
public class Lawyer extends Person { 

    @XmlElementWrapper(required = true) 
    @XmlElement(name = "lawyerOffice", namespace = "http://xxx/addressbook/external/v01/types") 
    protected List<LawyerOffice> lawyerOffices;  

    public List<LawyerOffice> getLawyerOffices() { 
    if (lawyerOffices == null) { 
     lawyerOffices = new ArrayList<LawyerOffice>(); 
    } 
    return lawyerOffices; 
    } 

    public void setLawyerOffices(List<LawyerOffice> lawyerOffices) { 
    this.lawyerOffices = lawyerOffices; 
    } 

} 

, 내가 얻을 :

{ 
    "ID": "e0d62504-4dfb-4c92-b70b-0d411e8ed102", 
    "lawyerOffice": [ 
     { 
      ... 
     } 
    ] 
} 

그래서 이름의 배열 변호사 사무소입니다. 나는 변호사 사무실 을 기대한다.

<dependency> 
     <groupId>com.fasterxml.jackson.jaxrs</groupId> 
     <artifactId>jackson-jaxrs-json-provider</artifactId> 
     <version>2.8.6</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
    </dependency> 

이 내 ObjectMapper 구성 (CXF 주입)입니다 : 나는 "올바른"목록 이름을 얻을 수있는 방법

@Provider 
@Consumes({ MediaType.APPLICATION_JSON, "text/json" }) 
@Produces({ MediaType.APPLICATION_JSON, "text/json" }) 
public class JsonProvider extends JacksonJsonProvider { 

    public static ObjectMapper createMapper() { 
    ObjectMapper mapper = new ObjectMapper(); 

    AnnotationIntrospector primary = new DPAJaxbAnnotationIntrospector(mapper.getTypeFactory()); 
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); 
    AnnotationIntrospector pair = AnnotationIntrospector.pair(primary, secondary); 
    mapper.setAnnotationIntrospector(pair); 
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 
    mapper.disable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED); 
    mapper.disable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); 
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); 

    return mapper; 

    } 

    public JsonProvider() { 
    super(); 

    this.setMapper(createMapper()); 

    } 

} 

은 내가 사용하는 구현을 무엇입니까?

+0

당신이 아래로 투표 코멘트를 남겨주세요 : 나는 objectMapper 구성에서 USE_WRAPPER_NAME_AS_PROPERTY_NAME 기능 옵션을 활성화. – mvermand

+1

'@XmlElement (name = "lawyerOffice"'... 'lawyerOffice'와 같은 소리는 '올바른 이름'입니다. 그렇다면 자신의 대답에 따라 원래의 ObjectMapper 구성 코드를 포함해야합니다. 실제로 문제가 JAXB 주석 지원을 명시 적으로 사용하여 발생했기 때문에 보이는 것처럼 보입니다.이 주석은 질문에서 제외하는 매우 중요한 세부 사항입니다. –

답변

1

해결책을 찾았습니다. 내가 질문을 적용 할 수 있도록

ObjectMapper mapper = new ObjectMapper(); 
    AnnotationIntrospector primary = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); 
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); 
    AnnotationIntrospector pair = AnnotationIntrospector.pair(primary, secondary); 
    mapper.setAnnotationIntrospector(pair); 
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 
    ... 
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); // <----- 
    ...