
日期作为参数与接口参数实体类的序列化不一致
问题描述
前端传参到后端服务一接口,服务一调用第二服务接口时,服务二接口接收日期参数序列化的时候报错了,具体报错内容如下:
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"])
原因分析
服务一和服务二接口使用的实体类字段都使用了 @jsonserialize 和 @jsondeserialize 注解,用来指定日期字段的序列化和反序列化方式。但是,两个接口中使用的注解规范不一致。
客户端:使用了 jsr-310 规范的 @jsondeserialize(using = localdatedeserializer.class) 注解
@jsondeserialize(using = localdatedeserializer.class) private localdate replacedate;
服务一:使用了 jsr-310 规范的 @jsonserialize(using = localdateserializer.class) 和 @jsondeserialize(using = localdatedeserializer.class) 注解
@jsonserialize(using = localdateserializer.class) @jsondeserialize(using = localdatedeserializer.class) private localdate replacedate;
服务二:使用了 hutool 工具包的序列化注解
@jsonfield(serializeusing = dateserializer.class) private localdate replacedate;
确保客户端、服务一和服务二的日期字段序列化和反序列化注解规范一致。建议使用 jsr-310 规范的注解:
@jsonserialize(using = localdateserializer.class) @jsondeserialize(using = localdatedeserializer.class) private localdate replacedate;
以上就是前后端日期参数序列化不一致导致接口调用报错:如何解决?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号