我们最近升级到了Spring Boot 2.0
,并因此升级了2.1
的JAX-RS和2.26
的Jackson。
现在的问题是,我尝试通过POST
(序列化和)发送的Java对象具有类型java.time.LocalDate
的字段,并且没有正确地序列化(成JSON)。这是更新后的结果:
...
"birthDate": {
"year": 1991,
"month": "JANUARY",
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
},
"monthValue": 1,
"dayOfMonth": 1,
"dayOfWeek": "TUESDAY",
"era": "CE",
"dayOfYear": 1,
"leapYear": false
},
...
...相反,birthDate
应该像以前"birthDate": "1991-01-01"
一样发送。
客户端配置为:
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
final JacksonJsonProvider provider = new JacksonJsonProvider();
provider.setMapper(mapper);
return ClientBuilder.newClient(new ClientConfig(provider))
.target("http://hostname/api/some-path")
.path("");
我还尝试注释了birthDate
提交的@JsonFormat(pattern = "yyyy-MM-dd")
。
答案 0 :(得分:1)
就我而言,升级后有效的是:
com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider
依赖项org.glassfish.jersey.media:jersey-media-json-jackson
依赖项:troll_face:
出现的编译问题JacksonJaxbJsonProvider
代替JacksonJsonProvider
-这对我来说完全是意外的最后,客户端的最终配置如下所示:
final ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return ClientBuilder.newClient() // ClientConfig if needed
.register(new JacksonJaxbJsonProvider(mapper, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS))
.target("http://hostname/api/some-path")
.path("");