Spring Mvc时间格式参数,前后时间传参,单个字段的时间格式化问题


不是在改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参数时上述注解无效。如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值