springboot通过javaconfig配置jackson

2020-10-18 09:52:07 浏览数 (1)

代码语言:javascript复制
@Configuration
public class JacksonConfig {
	// 配置特性属性值
    @Bean
    JacksonProperties jacksonProperties() {
        JacksonProperties properties = new JacksonProperties();
        properties.setDateFormat(DateHandler.DATETIME_PATTERN);
        properties.setJodaDateTimeFormat(DateHandler.DATETIME_PATTERN);
        properties.setTimeZone(TimeZone.getTimeZone(DateHandler.ZONEID));
        properties.getSerialization().put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return properties;
    }
	// 自定义特定类型的序列化反序列化器
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateHandler.DATE_FORMAT));
            builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateHandler.DATE_FORMAT));
            builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateHandler.TIME_FORMAT));
            builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateHandler.TIME_FORMAT));
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateHandler.DATETIME_FORMAT));
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateHandler.DATETIME_FORMAT));
        };
    }

}

0 人点赞