奇怪的JSON序列化与FasterXML杰克逊

问题描述:

我不明白为什么我得到序列化的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注释支持引起的,这是一个非常重要的细节,以避免你的问题。 –

我找到了解决方案。我能在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); // <----- 
    ...