不是在改bug,就是在写bug的路上
一、后台报错问题
Field error in object ‘leave’ on field ‘beginTime’: rejected value [2021-07-11 22:23:36]; codes [typeMismatch.leave.beginTime,typeMismatch.beginTime,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [leave.beginTime,beginTime]; arguments []; default message [beginTime]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.time.LocalDateTime’ for property ‘beginTime’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value ‘2021-07-11 22:23:36’; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2021-07-11 22:23:36]]
如果看着差不多就是我这个问题了,问题是前后端时间参数传递出错,字符串类型和LocalDateTime类型的冲突
报错时我的实体类
我的后台Controller
二、解决办法
给实体类字段添加注解
//@JsonFormat 单个字段出参格式化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
//@DateTimeFormat 单个字段入参格式化
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//可以同时使用也可以单独使用,根据情况而定
每一个LocalDateTime类型的都要加这个注解
上面写了单个的字段入参出参,等过两天记得写去全局的时间格式化问题
三、注意!!
根据个人本次经验:
如果Controller里面入参写了@RequestBody参数时上述注解无效。如下