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.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
/**
* Description:
* <p>
* date: 2023/5/9 14:38
*
* @author Arvin Lee
* @version 1.0
*/
@JsonComponent
public class JsonConfig {
/**
* 添加Long转json精度丢失的配置
*/
@Bean
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
SimpleModule module = new SimpleModule();
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(module);
return objectMapper;
}
}
后端Long类型传递前端精度丢失问题
最新推荐文章于 2025-04-24 10:32:34 发布
该配置类通过Jackson2ObjectMapperBuilder创建一个ObjectMapper实例,并注册一个SimpleModule,使用ToStringSerializer处理Long类型,防止在转换为JSON时精度丢失。
997

被折叠的 条评论
为什么被折叠?



