Spring MVC and Jackson mapping do not return the root element in json

后端 未结 4 1913
夕颜
夕颜 2021-01-06 12:09

I am having one issue with Spring MVC and its json support. I make one ajax call to get some data and I want to get that data in json format including the root value. I am

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 12:32

    It seems that the method withAnnotiationIntrospector does not set AnnotiationIntrospector. It's returns new DeserializationConfig/SerializationConfig object instead (with correct AnnotiationIntrospector).

    So, my version of JaxbJacksonObjectMapper:

    public class JaxbJacksonObjectMapper extends ObjectMapper {
    
        public JaxbJacksonObjectMapper() {
            super();
    
            final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    
            this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
            this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
    
            this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector));
            this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector));
    
        }
    }
    

    Now it support @XmlRootElement, @XmlTransient and other.

提交回复
热议问题