Spring Boot BigDecimal 返回时转为 String
Spring Boot About 577 words需求
金额字段如果以数字类型返回给前端时,JavaScript
可能会丢失精度,需在服务端转为String
保证精度不丢失。
代码
主要是simpleModule.addSerializer
添加BigDecimal
转为String
的处理方式ToStringSerializer
。
@Configuration
public class ObjectMapperConfig {
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}
Views: 798 · Posted: 2024-04-12
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...