Java异常之--Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800

本文目录

一、背景描述

二、现象描述

三、错误原因

四、解决方案


一、背景描述

环境:SpringCloud Fegin调用

服务端(被调用者):返回了一个JSON对象,其中有一个createTime字段,格式是java.sql.Timestamp。

客户端(调用者):使用对象整体接收参数,其中createTime字段对应类型是'yyyy-MM-dd HH:mm:ss'。

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800": not a valid representation (error: Failed to parse Date value '2020-04-27T19:43:05.000+0800': Unparseable date: "2020-04-27T19:43:05.000+0800"); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800": not a valid representation (error: Failed to parse Date value '2020-04-27T19:43:05.000+0800': Unparseable date: "2020-04-27T19:43:05.000+0800")
 
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800": not a valid representation (error: Failed to parse Date value '2020-04-27T19:43:05.000+0800': Unparseable date: "2020-04-27T19:43:05.000+0800")

二、现象描述

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800": not a valid representation (error: Failed to parse Date value '2020-04-27T19:43:05.000+0800': Unparseable date: "2020-04-27T19:43:05.000+0800"); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800": not a valid representation (error: Failed to parse Date value '2020-04-27T19:43:05.000+0800': Unparseable date: "2020-04-27T19:43:05.000+0800")
 at [Source: (PushbackInputStream); line: 1, column: 217] (through reference chain: com.uiotsoft.framework.core.response.CommResponse["data"]->com.uiotsoft.back.thirdpartycloud.api.response.SmsTemplateResponse["createTime"])
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:245)
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227)
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:102)
	... 37 common frames omitted
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-04-27T19:43:05.000+0800": not a valid representation (error: Failed to parse Date value '2020-04-27T19:43:05.000+0800': Unparseable date: "2020-04-27T19:43:05.000+0800")
 at [Source: (PushbackInputStream); line: 1, column: 217] (through reference chain: com.uiotsoft.framework.core.response.CommResponse["data"]->com.uiotsoft.back.thirdpartycloud.api.response.SmsTemplateResponse["createTime"])
	at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1549)
	at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:911)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:524)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:467)
	at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:195)
	at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:285)
	at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:268)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:530)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:528)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:417)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1287)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3084)
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239)
	... 39 common frames omitted

 

三、错误原因

调用者需要的格式是“yyyy-MM-dd HH:mm:ss”,而被调用者返回的确是java.sql.Timestamp类型的,类型不匹配,导致类型转换错误。

四、解决方案

在配置文件中加上如下配置即可

jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

spring:
  profiles:
    active: local
  cloud:
    zookeeper:
      discovery:
        enabled: true
        register: true
        root: /uiotsoft-service
      connectString: localhost:2181
  application:
    name: back-third-party-cloud-service
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

 

你可能感兴趣的:(异常解决方案)