问题:
客户端使用fastjson进行序列化,服务端使用jackson反序列化失败。报错。
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-12-25 10:07:49": not a valid representation (error: Failed to parse Date value '2018-12-25 10:07:49': Can not parse date "2018-12-25 10:07:49Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))
at [Source: {"acctBal":9797.11,"acctDate":"22222","acctName":"22222","acctNo":"xxxxxxxxxxxx","cretDebtFlag":"22222","currencyCode":"22222","insSeq":"22222","merId":"22222","merName":"22222","othAcctName":"22222","othAcctNo":"22222","othBankNo":"22222","remark":"22222","reqReserved":"22222","reserved":"22222","rtnReason":"22222","successtime":"2018-12-25 10:07:49","txnAmt":753375.33,"txnDate":"22222","txnNo":"22222","txnType":"22222"}; line: 1, column: 333] (through reference chain: com.baofoo.cgwfi.ws.wsdto.account.DownloadFileLzDetailDTO["successtime"])
at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:74)
at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1410)
at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:926)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:819)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:788)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:172)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:259)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:242)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
at com.baofoo.util.json.JsonCodingUtil.decode(JsonCodingUtil.java:45)
... 68 more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
原因
fastjson默认使用的序列化格式:
public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
1
jackson默认支持的日期反序列格式:
("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
1
jackson默认的日期反序列化不支持yyyy-MM-dd HH:mm:ss 这种格式,所以反序列化失败
解决办法一:重写jackson反序列化
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @version 1.0.0
* @description 重写反序列化
* @date 2018/12/25 9:51
**/
public class CustomJsonDateDeserializer extends JsonDeserializer
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = jp.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
在需要的date属性上添加注解:
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
private Date txnTime;
使用这种方式,需要重写上面反序列化抽象类
1
2
3
解决办法二:直接使用jackson注解,不需要重写反序列化
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date txnTime;
1
2
GMT+8表示时区,东八区
解决办法三:直接使用fastjson注解,不要重写反序列化
@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
private Date txnTime;
添加依赖:
————————————————
版权声明:本文为CSDN博主「EmineWang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a718515028/article/details/85245957