
接口接收参数序列化日期localdate的时候报错了?
问题:
前端向后端接口发送请求时,服务一调用服务二接口接收日期参数进行序列化的时候报错了。
问题详情:
- 前端传参:
- 服务一实体类和服务二实体类中日期字段均使用了相同的注解:
@notnull(message = "日期不能为空", groups = {addgroup.class})
@jsonserialize(using = localdateserializer.class)
@jsondeserialize(using = localdatedeserializer.class)
private localdate replacedate;- 服务一接口:
public zuojiankuohaophpcntyoujiankuohaophpcn resjsonzuojiankuohaophpcntyoujiankuohaophpcn addxxx(@validated({addgroup.class}) @requestbody xxxdto xxxdto) - 服务一调用服务二的方法:
public static zuojiankuohaophpcntyoujiankuohaophpcn string sendpost(t clazz, string url) {
jsonobject jsonobject = jsonutil.parseobj(clazz);
httpservletrequest request = servletutils.getrequest();
string authorization = request.getheader(constants.authorization);
return httputil.createpost(url)
.header(constants.authorization, authorization)
.body(jsonobject.tostring()).timeout(20000).execute().body();
}- 服务二接口:
public resjson getxxx(@requestbody xxxdto xxxdto)
报错内容:
org.springframework.http.converter.httpmessagenotreadableexception: json parse error: invalid value for year (valid values -999999999 - 999999999): 4665973755; nested exception is com.fasterxml.jackson.databind.jsonmappingexception: invalid value for year (valid values -999999999 - 999999999): 4665973755 (through reference chain: com.xxx.xxxdto["replacedate"])
at org.springframework.http.converter.json.abstractjackson2httpmessageconverter.readjavatype(abstractjackson2httpmessageconverter.java:391) ~[spring-web-5.3.22.jar:5.3.22]答案:
该问题是因为服务一中使用hutool的jsonutil 工具包反序列化导致的,这个工具包不是jsr310规范,而服务二中使用的是jsr310规范,导致序列化和反序列化规范不一致。只需将反序列化工具包替换为fastjson即可。
// JSONObject jsonObject = JSONUtil.parseObj(clazz); String json = JSON.toJSONString(clazz);










