2017-02-15 2 views
0

Ich verstehe nicht, warum ich den serialisierten JSON für die gegebene Klasse wie unten gezeigt bekomme.Seltsame JSON-Serialisierung mit FasterXML Jackson

Dies ist eine generierte Klasse von WSDL, also kann ich es nicht ändern:

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

} 

Wenn eine Klasseninstanz mit fasterxml.jackson serialisiert wird, erhalte ich:

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

So der Name Das Array ist RechtsanwaltOffice. Ich erwarte RechtsanwaltOffice s.

Dies ist die Implementierung, die ich benutze:

<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> 

Diese Konfiguration mein ObjectMapper wird (injiziert in 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()); 

    } 

} 

Wie kann ich die "richtige" Liste Namen?

+0

Bitte hinterlassen Sie einen Kommentar, wenn Sie unten Stimme, damit ich die Frage anpassen kann. – mvermand

+1

'@XmlElement (name =" anwalterOffice "' ..., klingt für mich wie 'rightyOffice' ist der 'richtige'name. Bei deiner eigenen Antwort solltest du wirklich den originalen' ObjectMapper' Konfigurationscode verwenden, weil es so aussieht, als ob Ihr Problem tatsächlich von Ihnen verursacht wird, indem Sie explizit die JAXB Annotation-Unterstützung verwenden, was ein ziemlich wichtiges Detail ist, um Ihre Frage nicht zu beantworten. –

Antwort

1

Ich fand die Lösung. Ich konnte die USE_WRAPPER_NAME_AS_PROPERTY_NAME Feature-Option in der objectMapper Konfiguration:

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