本文介绍了序列化对象时,如何强制杰克逊将数字写为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个 java 对象上有一个非常大的 id.当 jackson 将其转换为 JSON 时,它会将它作为数字发送(例如 {"id":1000110040000000001}),但一旦它成为 javascript 对象,id 就会更改为 1000110040000000000.我阅读了有关此问题的信息 这里

I have an id that is pretty large on one of my java objects. When it jackson converts it to JSON it sends it down as a number (e.g. {"id":1000110040000000001}) but as soon as it becomes a javascript object the id gets changed to 1000110040000000000. I read about this issue here

当 id 较小时它工作正常.我的第一个想法是强迫杰克逊将所有数字转换为字符串,但我也愿意接受其他选择.如果可能,我不想在我的 java 对象中添加 Jackson 注释.

It works fine when the id is smaller. My first thought is to just force Jackson to convert all the numbers to strings but I am open to other options as well. If possible I would prefer not to add Jackson annotations to my java objects.

推荐答案

Jackson-databind(至少 2.1.3)提供了特殊的 ToStringSerializer.对我来说就是这样.

Jackson-databind (at least 2.1.3) provides special ToStringSerializer. That did it for me.

@Id @JsonSerialize(using = ToStringSerializer.class)
private Long id;

这篇关于序列化对象时,如何强制杰克逊将数字写为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:56