目录 一. 报错信息二. 版本信息三. 解决方法1. 使用@JSONSerialize + @jsonDeserialize注解2. 回退Spring Boot版本3. 回退jackson版本 四. 一些尝试(未解决问
最近新开了一个测试项目,使用了Java8的LocalDateTime替换了之前使用的Date类。接口返回结果时,抛出了序列化异常:
java.lang.reflect.UndeclaredThrowableException: null…………Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: cn.xx.dao.common.Back["data"]->cn.xx.dao.data.vo.BmVo["spe"]->cn.xx.dao.entity.Spe["createTime"])
spring boot:2.5.1
jackson-databind:2.12.3
……<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.5.1version> <relativePath/> parent>……<dependencies> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.12.3version> <scope>compilescope> dependency>dependencies>
<dependency> <groupId>com.fasterxml.jackson.datatypegroupId> <artifactId>jackson-datatype-jsr310artifactId> <version>2.12.3version>dependency>
@JsonSerialize(using = LocalDateTimeSerializer.class)@JsonDeserialize(using = LocalDateTimeDeserializer.class)private LocalDateTime createTime;
这时序列化正常,接口返回了结果,但是很明显不是我们需要的结果:
{"createTime": [2023,3,20,17,34,37]}
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")@JsonSerialize(using = LocalDateTimeSerializer.class)@JsonDeserialize(using = LocalDateTimeDeserializer.class)private LocalDateTime createTime;
InvalidDefinitionException这个报错,大部分博客提供的思路都是这个,理论上没什么问题,但是实际情况可能需要全局处理如LocalDateTime的时间格式,如果为相关的时间字段一个个的加注解,又太过麻烦。所以这种方法只能用于需要处理特定时间格式的字段上。
鉴于方法A只能作用于局部,而使用Jackson2ObjectMapperBuilderCustomizer时,仍然无法解决InvalidDefinitionException这个异常问题,只能将Spring Boot回退至2.4.x版本,问题得到解决。
参考:Spring Boot 2.5.0 and InvalidDefinitionException: Java 8 date/time type java.time.Instant not supported by default
解决流程:
@Configurationpublic class GlobalDateConfig {// 序列化时设置的时间格式private final String formatter = "yyyy-MM-dd HH:mm:ss"; @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern(formatter); //返回时间数据序列化 builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(localDateTimeFormatter)); // 接收时间数据反序列化 builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(localDateTimeFormatter)); }; }}
……<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.4.2version> <relativePath/> parent>……
Spring Boot 2.4.x使用的是jackson 2.11.x
Spring Boot 2.5.x使用的是jackson 2.12.x
参考:Spring Boot 2.5.0 and InvalidDefinitionException: Java 8 date/time type java.time.Instant not supported by default
<dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.11.2version>dependency><dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-coreartifactId> <version>2.11.2version>dependency><dependency> <groupId>com.fasterxml.jackson.datatypegroupId> <artifactId>jackson-datatype-jsr310artifactId> <version>2.11.2version>dependency>
根据Spring Boot的讨论和Stack overflow的一些解决方法做了一些尝试,但是均未成功解决问题。
我的项目中,并不存在这种自定义ObjectMapper且将其设置为全局Bean的情况。
如果存在自定义ObjectMapper Bean的情况下,需要注册处理java8时间序列化的类:JavaTimeModule(或者jdk8Module)
@Bean@Primarypublic ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.reGISterModule(new JavaTimeModule()); return objectMapper;}
或者:
@Bean@Primarypublic ObjectMapper objectMapper() { return JsonMapper.builder() .addModule(new JavaTimeModule()) .build();}
或者
@Resourceprivate Jackson2ObjectMapperBuilder mapperBuilder;@Bean@Primarypublic ObjectMapper objectMapper() { ObjectMapper build = mapperBuilder.build(); objectMapper.registerModule(new JavaTimeModule()); return objectMapper;}
这几种情况没遇到过,不知道是否可行。总之这里对我的代码不起作用。
此外,JavaTimeModule还支持自定义格式
DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern(formatter);// 添加Java8时间序列化反序列化JavaTimeModule javaTimeModule = new JavaTimeModule();//返回时间数据序列化javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(localDateTimeFormatter));//接收时间数据反序列化javaTimeModule.aDDDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(localDateTimeFormatter));
很明显,这个就是实现一个Jackson2ObjectMapperBuilderCustomizer类,与上文中的方法jackson2ObjectMapperBuilderCustomizer作用相同。这里也无法解决开头的问题。
@Configurationpublic class GlobalDateConfig implements Jackson2ObjectMapperBuilderCustomizer { private final String formatter = "yyyy-MM-dd HH:mm:ss"; @Override public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { jacksonObjectMapperBuilder.simpleDateFormat(formatter) .modules(new JavaTimeModule()) .serializationInclusion(JsonInclude.Include.ALWAYS) .failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToEnable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN) .featuresToEnable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS); }}
--结束END--
本文标题: 解决报错InvalidDefinitionException Java 8 date/time type LocalDateTime not supported by default jsr310
本文链接: https://lsjlt.com/news/382844.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0