在使用jackson对于包含使用LocalDate类型的java对象进行序列化时报错如下:
jakarta.json.JsonException: Jackson exception
at co.elastic.clients.json.jackson.JacksonUtils.convertException(JacksonUtils.java:39)
at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:100)
at co.elastic.clients.util.BinaryData.of(BinaryData.java:80)
at co.elastic.clients.transport.rest_client.RestClientTransport.collectNdJsonLines(RestClientTransport.java:273)
at co.elastic.clients.transport.rest_client.RestClientTransport.collectNdJsonLines(RestClientTransport.java:269)
at co.elastic.clients.transport.rest_client.RestClientTransport.prepareLowLevelRequest(RestClientTransport.java:225)
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:153)
at co.elastic.clients.elasticsearch.ElasticsearchClient.bulk(ElasticsearchClient.java:313)
at com.cxstar.business.service.impl.CanalClientServiceImpl.save(CanalClientServiceImpl.java:68)
at com.cxstar.business.es.canal.CanalClient.parseEntry(CanalClient.java:159)
at com.cxstar.business.es.canal.CanalClient.run(CanalClient.java:92)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:808)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:798)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:350)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1359)
at com.cxstar.ApiApplication.main(ApiApplication.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDate` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.cxstar.business.es.entity.WorksEs["publishDate"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1276)
at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3126)
at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:98)
... 20 more
报错原因是在序列化对象时涉及到 Java 8 的日期/时间类型 java.time.LocalDate,但默认情况下 Jackson 不支持这种类型。要解决这个问题,可以添加 Jackson 模块 “com.fasterxml.jackson.datatype:jackson-datatype-jsr310”,以启用对 Java 8 日期/时间类型的支持。
在 Maven 项目中pom.xml 文件中添加以下依赖
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.2</version>
</dependency>
字段添加注解: