springboot+jersay环境下解决long类型到前台精度丢失的问题

项目采用前后端分离的方式开发,后台接口开发好后通过postman也测试通过了,但是集成前端的时候发出,后台LONG类型的数据到了前台后数度丢失了:

后台数据:

102010_RfSs_3295928.png

前台:

102059_rUaG_3295928.png

最后一位精度丢失。

主要原因可能是js内部的数据表示问题,具体原因还不太了解。

解决方法:

将long类型的数据转为string类型。

可通过自定义spring ObjectMapper来统一实现:

@EnableTransactionManagement
@MapperScan("com.makeronly.*.repo")
@SpringBootApplication
public class Application{

    /**
     * 防止json时出现错误FAIL_ON_EMPTY_BEANS
     * @return
     */
    @Bean
    public ObjectMapper objectMapper() {
        SimpleModule simpleModule = new SimpleModule();   simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        ObjectMapper myObjectMapper = new ObjectMapper();
        myObjectMapper.registerModule(simpleModule);
        myObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        return myObjectMapper;
    }

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }

主要是标红的这几句话实现。

转载于:https://my.oschina.net/u/3295928/blog/1784842

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值