@XMLElementWrapper issue with com.fasterxml.jackson JacksonJaxbJsonProvider

非 Y 不嫁゛ 提交于 2019-12-31 03:18:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!