1.springmvc对Date类型参数的处理
1.1先看下保错信息
2019-01-07 10:24:02.694 INFO 13000 --- [nio-8101-exec-1] c.x.k.l.global.GlobalResponseHandler : JSON parse error: Can not deserialize value of type java.util.Date from String "2019-1-7 10:21:00": not a valid representation (error: Failed to parse Date value '2019-1-7 10:21:00': Can not parse date "2019-1-7 10:21:00Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2019-1-7 10:21:00": not a valid representation (error: Failed to parse Date value '2019-1-7 10:21:00': Can not parse date "2019-1-7 10:21:00Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))
用来接收参数的PO:
1.2 解决方案
Date类型的参数字段上加上一个注解: @JsonFormat( pattern="yyyy-MM-dd HH:mm:ss")
这个注解的作用:
或者使用这个注解:
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss" )