SpringBoot框架下传Long类型到前端导致JS精度损失解决方案

在Spring框架中可以写个Config类来进行全局配置,将所有Long类型数据序列化返回成String类型,使用了Jackson

@Configuration
public class JacksonConfig {
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)  {
        // 全局配置序列化返回 JSON 处理
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        SimpleModule simpleModule = new SimpleModule();
        //JSON Long ==> String
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}

 

posted @ 2021-08-16 10:06  onecyl  阅读(120)  评论(0编辑  收藏  举报