博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot+jersay环境下解决long类型到前台精度丢失的问题
阅读量:6998 次
发布时间:2019-06-27

本文共 986 字,大约阅读时间需要 3 分钟。

hot3.png

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

后台数据:

102010_RfSs_3295928.png

前台:

102059_rUaG_3295928.png

最后一位精度丢失。

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

解决方法:

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

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

@EnableTransactionManagement@MapperScan("com.makeronly.*.repo")@SpringBootApplicationpublic 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

你可能感兴趣的文章