问题
I am trying to get the correct JSON for
public class MyTestResponse {
@XmlElementWrapper(name = "data")
@XmlElement(name = "values")
public List<String> test = Arrays.asList("Sidney");
}
I now get
"values": [
"Sidney"
],
instead of
"data":{
"values": [
"Sidney"
]
},
So the wrapper element "data" is not present.
I am using com.fasterxml.jackson stack (2.8.6) inside ServiceMix 7 M3.
My JSON provider extends com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider:
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
@Provider
@Consumes({ MediaType.APPLICATION_JSON, "text/json" })
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class JsonProvider extends JacksonJaxbJsonProvider {
public JsonProvider() {
super();
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.disable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
mapper.disable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
this.setMapper(mapper);
}
}
How can I tell the JacksonJaxbJsonProvider to render the wrapper element around the values element?
来源:https://stackoverflow.com/questions/41959368/xmlelementwrapper-issue-with-com-fasterxml-jackson-jacksonjaxbjsonprovider