2017-02-15 81 views
0

我不明白爲什麼我得到序列化的JSON爲給定的類如下所示。奇怪的JSON序列化與FasterXML傑克遜

這是從WSDL生成的類,所以我不能改變它:

@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; 
    } 

} 

當一個類實例獲取與fasterxml.jackson連載,我得到:

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

這樣的名字該陣列是lawyerOffice。我期待lawyerOffice s

這是我使用的實現:

<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

請發表評論時,你失望的投票,所以我可以適應的問題。 – mvermand

+1

'@XmlElement(name =「lawyerOffice」'...,聽起來像'lawyerOffice'是'正確'的名字,也就是說,考慮到你自己的答案,你應該真的包含原始的'ObjectMapper'配置代碼,因爲它看起來像你的問題實際上是由你明確使用JAXB註釋支持引起的,這是一個非常重要的細節,以避免你的問題。 –

回答

1

我找到了解決方案。我能在objectMapper配置USE_WRAPPER_NAME_AS_PROPERTY_NAME功能選項:

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); // <----- 
    ...