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

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh