I don't understand why I get the serialized JSON for the given Class as shown below.
This is a generated class from WSDL, so I cannot change it:
@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;
}
}
When a class instance gets serialized with fasterxml.jackson, I get:
{
"ID": "e0d62504-4dfb-4c92-b70b-0d411e8ed102",
"lawyerOffice": [
{
...
}
]
}
So the name of the array is lawyerOffice . I expect lawyerOffice s .
This is the implementation that I use:
<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>
This is my ObjectMapper configuration (injected 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());
}
}
How can I get the "correct" list name?
I found the solution. I enabled the USE_WRAPPER_NAME_AS_PROPERTY_NAME
feature option in the objectMapper configuration:
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); // <-----
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.