无法使用Jackson将java

无法使用Jackson将java

本文介绍了无法使用Jackson将java.time.LocalDate序列化为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是带有jackson 2.6.2的spring-boot 1.2.1.RELEASE,其中包括jsr310数据类型.我正在使用注释@SpringBootApplication启动Spring应用程序.我有

I am using spring-boot 1.2.1.RELEASE with jackson 2.6.2 including the jsr310 datatype. I am using the annotation @SpringBootApplication to kick off my Spring app. I have

spring.jackson.serialization.write_dates_as_timestamps = false

在我的application.properties中设置(由于正在使用banner = false进行测试,因此我正在读取该信息.)

set in my application.properties (which I know is being read because I tested with banner = false).

但是java.time.LocalDate仍被序列化为整数数组.我没有使用@EnableWebMvc.

And yet java.time.LocalDate is still being serialized as an array of integers. I am not using @EnableWebMvc.

好像我添加了标签

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")

到我的LocalDate变量即可.但是我以为上面的属性集是自动的.另外,如果我没记错的话(从那以后我就决定使用整数数组),那只适用于序列化而不是反序列化(但是我不能坦白地记得最后一部分是否正确).

to my LocalDate variable then it works. But I thought it was automatic with the above property set. Plus, if I remember right (I've since just decided to work with the integer array), that only worked with serialization and not deserialization (but I can't honestly quite remember if that last part is true).

推荐答案

这是知道问题.您需要手动执行.

This is know issue in Spring Boot. You need to do it manually.

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

或更新至1.2.2

更新:还有一种配置ObjectMapper 由弹簧从容器中使用.

UPDATE:Also there is a way to configure ObjectMapper used by spring from your container.

这篇关于无法使用Jackson将java.time.LocalDate序列化为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:50