无法从 String 反序列化 `java.util.Date` 类型的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55227544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:11:19  来源:igfitidea点击:

Cannot deserialize value of type `java.util.Date` from String

javaspring-bootspring-mvcHymansonHymanson-databind

提问by Utkarsh Gandhi

Using Spring 1.5.8.RELEASE Hymanson mapper giving the following exception.

使用 Spring 1.5.8.RELEASE Hymanson 映射器给出以下异常。

Cannot deserialize value of type `java.util.Date` from String "2018-09-04T10:44:46": expected format "yyyy-MM-dd'T'HH:mm:ss.SSS"

at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.copart.conversationapi.calldisposition.model.vo.CallLogEntity["callEndTime"])

在 [来源:未知;行:-1,列:-1](通过参考链:com.copart.conversationapi.calldisposition.model.vo.CallLogEntity["callEndTime"])

CallEntity.java

调用实体

@JsonProperty("callEndTime")
@Column(name = "call_end_ts")
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS")
private Date callEndTime;

DAO.java

DAO.java

ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> finalHashMap;
finalHashMap = convertMultiToString(requestMap);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
CallLogEntity callLogEntity = mapper.convertValue(finalHashMap, CallEntity.class);

pom.xml

pom.xml

<dependency>
     <groupId>com.fasterxml.Hymanson.core</groupId>
     <artifactId>Hymanson-databind</artifactId>
     <version>2.9.0</version>
     <exclusions>
        <exclusion>
           <groupId>com.fasterxml.Hymanson.core</groupId>
           <artifactId>Hymanson-core</artifactId>
        </exclusion>
        <exclusion>
           <groupId>com.fasterxml.Hymanson.core</groupId>
           <artifactId>Hymanson-annotations</artifactId>
        </exclusion>
     </exclusions>
  </dependency>

采纳答案by mkhanoyan

Change your @JsonFormatline to this.

将您的@JsonFormat线路更改为此。

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")

The format pattern you have right now expects the sting to have millisecond values - but your example string doesn't have them.

您现在拥有的格式模式期望 sting 具有毫秒值 - 但您的示例字符串没有它们。