How to configure jackson with spring globally?

前端 未结 4 378
走了就别回头了
走了就别回头了 2021-01-20 12:47

To serialize deserialize object I am useing Jackson as flow

@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.         


        
4条回答
  •  粉色の甜心
    2021-01-20 13:16

    With Spring Boot you can achieve this by registering new Module.

    @Configuration
    public class AppConfig {
    
        @Bean
        public Module module() {
            SimpleModule module = new SimpleModule("Module", new Version(1, 0, 0, null, null, null));
            module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
            module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
    
            return module;
        }
    }
    

    As stated in documentation here

    Jackson 1.7 added ability to register serializers and deserializes via Module interface. This is the recommended way to add custom serializers -- all serializers are considered "generic", in that they are used for subtypes unless more specific binding is found.

    and here:

    Any beans of type com.fasterxml.jackson.databind.Module are automatically registered with the auto-configured Jackson2ObjectMapperBuilder and are applied to any ObjectMapper instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.

提交回复
热议问题