接口接收参数序列化日期localdate的时候报错了?
问题:
前端向后端接口发送请求时,服务一调用服务二接口接收日期参数进行序列化的时候报错了。
问题详情:
@notnull(message = "日期不能为空", groups = {addgroup.class}) @jsonserialize(using = localdateserializer.class) @jsondeserialize(using = localdatedeserializer.class) private localdate replacedate;
public <t> resjson<t> addxxx(@validated({addgroup.class}) @requestbody xxxdto xxxdto)
public static <t> 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);
以上就是LocalDate日期序列化失败:服务间调用时如何解决JSON解析错误?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号