while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSSZ‘, parsing fails (leniency? null))

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSSZ‘, parsing fails (leniency? null)),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

我们知道前端提交字符串到后台映射为Date类型可以加上@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")注解,但是这次加了还是报错

前端提交字符串到后台,出现如下错误:

while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))

在这里插入图片描述
错误的大致意思就是字符串映射到Date类型失败,说我们的格式的确是符合的,但是还是映射失败

完整错误:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2020-07-31 07:25:29": not a valid representation (error: Failed to parse Date value '2020-07-31 07:25:29': Cannot parse date "2020-07-31 07:25:29": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-07-31 07:25:29": not a valid representation (error: Failed to parse Date value '2020-07-31 07:25:29': Cannot parse date "2020-07-31 07:25:29": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
 at [Source: (PushbackInputStream); line: 1, column: 87] (through reference chain: com.xb.entity.Meeting["endTime"])
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241)
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:223)
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:206)
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
	at
	..........

我们知道前端提交字符串到后台映射日期类型的话,加上@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")注解就行了,本人亲测SpringMVC环境可行,但此次报错的项目是SpringBoot项目,不知道是SpringBoot项目导致的转换失败还是SpringBoot项目所使用的SpringMVC版本太高才导致转换失败(这个有兴趣的小伙伴可以测试)

  • 解决方案:

application.yml中添加如下配置

spring: 
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/131817.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!