与JS.fasterxml.jackson JacksonJaxbJsonProvider的@XMLElementWrapper问题

时间:2017-01-31 13:55:47

标签: java json jackson fasterxml

我正在尝试为

获取正确的JSON
public class MyTestResponse {
    @XmlElementWrapper(name = "data")
    @XmlElement(name = "values")
    public List<String> test = Arrays.asList("Sidney");
}

我现在得到了

"values": [
    "Sidney"
],

而不是

"data":{
    "values": [
        "Sidney"
    ]
},

所以包装元素&#34;数据&#34;不存在。

我在ServiceMix 7 M3中使用com.fasterxml.jackson stack(2.8.6)。

我的JSON提供程序扩展了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);

    }

}

如何告诉JacksonJaxbJsonProvider在values元素周围渲染包装元素?

0 个答案:

没有答案