Where do I specify Jackson SerializationConfig.Feature settings in Spring 3.1

后端 未结 1 938
猫巷女王i
猫巷女王i 2020-12-10 14:48

I\'m puzzled as to why using a default inclusion of jackson that Spring seems to have customised the default Jackson configuration.

One setting it\'s messing with is

相关标签:
1条回答
  • 2020-12-10 15:29

    Starting with 3.1 M1 you can specify jackson custom configuration by registering an HttpMessageConverters through a sub-element of mvc:annotation-driven.

    See Spring 3.1 MVC Namespace Improvements

    See SPR-7504 Make it easier to add new Message Converters to AnnotationMethodHandlerAdapter

    Exemple:

    <bean id="jacksonObjectMapper" class="x.y.z.CustomObjectMapper">                
    </bean>
    
    <mvc:annotation-driven>
        <mvc:message-converters>
           <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
               <property name="objectMapper" ref="jacksonObjectMapper" />
           </bean>
           </mvc:message-converters>
    </mvc:annotation-driven>
    

    The CustomObjectMapper Object

        @Component("jacksonObjectMapper")
        public class CustomObjectMapper extends ObjectMapper {
    
            @PostConstruct
            public void afterPropertiesSet() throws Exception {
    
                SerializationConfig serialConfig = getSerializationConfig()     
                            .withDateFormat(null);
    
                      //any other configuration
    
                this.setSerializationConfig(serialConfig);
            }
        }
    

    SerializationConfig .withDateFormat

    In addition to constructing instance with specified date format, will enable or disable Feature.WRITE_DATES_AS_TIMESTAMPS (enable if format set as null; disable if non-null)

    0 讨论(0)
提交回复
热议问题