后端报错:
2024-02-14 16:33:44.715 ERROR 968 --- [nio-8080-exec-8] c.pxl.exception.GlobalExceptionHandler : runtimeException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not a valid representation (error: Failed to parse Date value 'Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)': Cannot parse date "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not a valid representation (error: Failed to parse Date value 'Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)': Cannot parse date "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
前端返回过来的数据无法解析JSON格式,可以配置一个日期反序列化类进行配置。
具体类型需要看你要什么再来详细配置
java
package com.pxl.config;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
// 自定义日期反序列化类
public class CustomDateDeserializer extends JsonDeserializer<Date> {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, IOException {
String dateStr = jsonParser.getText();
try {
return (Date) dateFormat.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date: " + dateStr, e);
}
}
}