Jackson 转换中 关于 浮点数处理的问题

结论:需要返回给前端的API的DTO中的浮点数属性,推荐使用BigDecimal,使用double会有变成科学计数法的问题;

其他情况,rpc通信或者应用间的序列化反序列化传输,double与BigDecimal均可;

BigDecimal初始化请使用 BigDecimal.valueOf(xx),切记用字符串来初始化,保证精度。

I think I found it: On my first search I missed that Jackson serialization had both SerializationFeatures and DeserializationFeatures, and they are a little different. According to http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/DeserializationFeature.html there is a feature called USE_BIG_DECIMAL_FOR_FLOATS

Feature that determines whether JSON floating point numbers are to be deserialized into BigDecimals if only generic type description (either Object or Number, or within untyped Map or Collection context) is available.

So in my case it was basically

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

JsonRpcHttpClient rpcHttpClient = new JsonRpcHttpClient(
    mapper,
    new URL("the url"),
    new HashMap<String, String>()); 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

https://blog.csdn.net/fxtxz2/article/details/109057340

application.properties

spring.jackson.deserialization.USE_BIG_DECIMAL_FOR_FLOATS=true
spring.jackson.serialization.WRITE_BIGDECIMAL_AS_PLAIN=true

参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值