Java 使用FasterXML实现奇怪的JSON序列化

Java 使用FasterXML实现奇怪的JSON序列化,java,json,serialization,jackson,fasterxml,Java,Json,Serialization,Jackson,Fasterxml,我不明白为什么我得到给定类的序列化JSON,如下所示 这是从WSDL生成的类,因此我无法更改它: @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Lawyer") public class Lawyer extends Person { @XmlElementWrapper(required = true) @XmlElement(name = "lawyerOffice", namespace = "http://

我不明白为什么我得到给定类的序列化JSON,如下所示

这是从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;
    }

}
因此,数组的名称为律师事务所。我希望律师事务所

这是我使用的实现:

    <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配置中启用了
USE\u WRAPPER\u NAME\u AS\u PROPERTY\u 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); // <-----
    ...
ObjectMapper mapper=new ObjectMapper();
AnnotationIntrospector primary=新的JaxbAnnotationIntrospector(mapper.getTypeFactory());
AnnotationIntrospector secondary=新的JacksonAnnotationIntrospector();
AnnotationIntroscoctor对=AnnotationIntroscoctor.pair(主、辅);
mapper.SetAnnotationIntrosector(成对);
setSerializationInclusion(JsonInclude.Include.NON_NULL);
...

mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);//当你否决投票时,请留下评论,这样我可以修改这个问题。
@xmlement(name=“lawyerOffice”
…,在我看来,
律师事务所
是一个“正确”的名字。也就是说,根据您自己的回答,您真的应该包括原始的
ObjectMapper
配置代码,因为看起来您的问题实际上是由您显式使用JAXB注释支持引起的,这是一个非常重要的细节别提你的问题。
@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());

    }

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