300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > LocalDateTime日期转换错误:JSON parse error: Cannot deserialize value of type java.time.LocalDateTime

LocalDateTime日期转换错误:JSON parse error: Cannot deserialize value of type java.time.LocalDateTime

时间:2020-01-25 00:42:05

相关推荐

LocalDateTime日期转换错误:JSON parse error: Cannot deserialize value of type java.time.LocalDateTime

LocalDateTime日期转换错误:JSON parse error: Cannot deserialize value of type java.time.LocalDateTime

背景:实体类日期字段使用LocalDateTime,使用Postman测试时传入 xxxx-xx-xx 00:00:00 报错方案一:实体类字段格式化日期方案二:增加日期转换配置类

背景:实体类日期字段使用LocalDateTime,使用Postman测试时传入 xxxx-xx-xx 00:00:00 报错

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of typejava.time.LocalDateTimefrom String “-12-01 00:00:00”: Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘-12-01 00:00:00’ could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of typejava.time.LocalDateTimefrom String “-12-01 00:00:00”: Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘-12-01 00:00:00’ could not be parsed at index 10\n at [Source: (PushbackInputStream); line: 1, column: 87] (through reference chain: cn.shuchuang.birdev.system.entity.Activity[“startTime”])

方案一:实体类字段格式化日期

每个实体类的日期字段都要格式化一次,配置灵活,但重复工作量大。

@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime birthday;@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime modify;

方案二:增加日期转换配置类

一次配置全局生效,但原日期格式 -12-16T09:31:08.923Z 将不再生效,且会报错。

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.convert.converter.Converter;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;@Configurationpublic class LocalDateTimeSerializerConfig {private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";private static final String DATE_PATTERN = "yyyy-MM-dd";/*** string转localdate*/@Beanpublic Converter<String, LocalDate> localDateConverter() {return new Converter<String, LocalDate>() {@Overridepublic LocalDate convert(String source) {if (source.trim().length() == 0) {return null;}try {return LocalDate.parse(source);} catch (Exception e) {return LocalDate.parse(source, DateTimeFormatter.ofPattern(DATE_PATTERN));}}};}/*** string转localdatetime*/@Beanpublic Converter<String, LocalDateTime> localDateTimeConverter() {return new Converter<String, LocalDateTime>() {@Overridepublic LocalDateTime convert(String source) {if (source.trim().length() == 0) {return null;}// 先尝试ISO格式: -07-15T16:00:00try {return LocalDateTime.parse(source);} catch (Exception e) {return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DATE_TIME_PATTERN));}}};}/*** 统一配置*/@Beanpublic Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {JavaTimeModule module = new JavaTimeModule();LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);return builder -> {builder.simpleDateFormat(DATE_TIME_PATTERN);builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_PATTERN)));builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));builder.modules(module);};}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。