我们在做springboot项目时,有时会遇到JavaScript解析java long类型数据时 丢失精度的问题,比如:
按下F12, 前端页面显示的ids:
数据库表中显示的ids:
两者不符,JavaScript解析时进行了四舍五入,丢失了精度,会导致执行sql不成功.
这时可以添加一个JacksonConfig类,来解决这个问题:
package warren.reggie.common;
/*
* author: Warren
*/
/**
* 配置类:解决前端传递 Java `long` 类型数据时,可能由于 JavaScript 精度限制导致的精度丢失问题。
* 该配置类通过自定义 `ObjectMapper`,将 Java 中的 `Long` 类型序列化为 `String` 类型,
* 以避免 JavaScript 处理大数时的精度丢失。
*/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class JacksonConfig {
/**
* 定义一个 `ObjectMapper` bean,用于全局自定义 JSON 处理方式。
* 在 Spring Boot 启动时,自动配置并注入该 `ObjectMapper`。
* 如果没有其他 `ObjectMapper` Bean,才会使用该配置。
*
* 通过这个配置,所有的 `Long` 类型字段在序列化时都会被转换为 `String`,
* 这样可以避免 JavaScript 在处理大数时的精度丢失。
*
* @param builder 用于创建 `ObjectMapper` 的构建器
* @return 自定义的 `ObjectMapper` 实例
*/
@Bean
@Primary // 该 `ObjectMapper` Bean 是首选的,优先于其他定义的 `ObjectMapper` 被使用
@ConditionalOnMissingBean(ObjectMapper.class) // 只有在没有其他 `ObjectMapper` Bean 的情况下才会生效
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
// 使用 Jackson2ObjectMapperBuilder 创建一个基本的 ObjectMapper 实例
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 创建一个 SimpleModule 来配置自定义的序列化规则
SimpleModule simpleModule = new SimpleModule();
// 将 Long 类型的字段序列化为 String 类型,以避免精度丢失
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
// 将该自定义模块注册到 ObjectMapper 中
objectMapper.registerModule(simpleModule);
// 返回自定义的 ObjectMapper 实例
return objectMapper;
}
}