springboot通过javaconfig配置jackson

@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));
        };
    }

}

posted on 2020-10-10 09:47  路过君  阅读(778)  评论(0编辑  收藏  举报

导航