踩坑记录:Date接收yyyy-MM-dd HH:mm:ss失败

一、问题描述

Date 接收 2021-03-26 13:17:11 失败

2021-03-26 13:34:04.553  WARN 6744 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2021-03-26 13:17:11": not a valid representation (error: Failed to parse Date value '2021-03-26 13:17:11': Cannot parse date "2021-03-26 13:17:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2021-03-26 13:17:11": not a valid representation (error: Failed to parse Date value '2021-03-26 13:17:11': Cannot parse date "2021-03-26 13:17:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))at [Source: (PushbackInputStream); line: 14, column: 16]

二、原因

json只支持一下几种格式

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
"EEE, dd MMM yyyy HH:mm:ss zzz"
"yyyy-MM-dd"

三、解决方案

添加配置

spring:
  mvc:
    date-format: yyyy-MM-dd HH:mm:ss
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
    deserialization:
      accept_empty_string_as_null_object: true
上一篇:SpringBoot2.X中lagback详细配置及使用


下一篇:前后端Date类型装换