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

后端 未结 4 1943
夕颜
夕颜 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:36

    It seems that the spring configuration was not right. That did not override the messageconverters, so it was not taking the Mapper that I wrote.

    To override those, the right spring config is:

        
        
            
                
            
        
    
    

    Now I am getting the root element, but it is not taking the name given in the jabx annotation, but the name of the class.

    It seems that the new method (withAnnotiationIntrospector) does not work as it should, or I am missing something. But if use the deprecated one, I get the proper root name defined in the JABX label.

        public JaxbJacksonObjectMapper() {
        final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    
        this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
    
        // When using the non deprecated method (withAnnotationIntrospector),
        // there are problems with JAXB/JSON parser so don't use right now
    
        super.getDeserializationConfig().setAnnotationIntrospector(introspector);
        super.getSerializationConfig().setAnnotationIntrospector(introspector);
    }
    

提交回复
热议问题