Cannot deserialize value of type `java.sql.Timestamp` from String "2023-05-23 16:53:28": not a valid representation (error: Failed to parse Date value '2023-05-23 16:53:28': Cannot parse date "2023-05-23 16:53:28": 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.sql.Timestamp` from String "2023-05-23 16:53:28": not a valid representation (error: Failed to parse Date value '2023-05-23 16:53:28': Cannot parse date "2023-05-23 16:53:28": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
时间: 2023-12-03 21:43:05 浏览: 680
the string was not recognized as a valid Datetime.
5星 · 资源好评率100%
这个错误是因为在反序列化过程中,Jackson库无法将字符串 "2023-05-23 16:53:28" 转换为 java.sql.Timestamp 对象。这通常是由于日期字符串的格式与 Timestamp 的默认格式不匹配导致的。
你可以尝试更改日期字符串的格式,使其与 Timestamp 的默认格式匹配。或者,你可以创建一个自定义的反序列化器来处理此日期格式。
以下是一个示例自定义反序列化器:
```
public class TimestampDeserializer extends JsonDeserializer<Timestamp> {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Timestamp deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String dateString = jsonParser.getText();
try {
Date date = dateFormat.parse(dateString);
return new Timestamp(date.getTime());
} catch (ParseException e) {
throw new IOException("Error parsing date string: " + dateString, e);
}
}
}
```
然后在你的实体类中使用此自定义反序列化器:
```
@JsonDeserialize(using = TimestampDeserializer.class)
private Timestamp timestampField;
```
阅读全文