ObjectMapper默认Number转换为Double解决方式

ObjectMapper默认Number转换为Double解决方式:

设置ObjectMapper的默认转换方式

ObjectMapper mapper = new ObjectMapper()mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

所有配置代码:

@CompileStatic
@TypeChecked
@Configuration
class RestTemplateConfig {

    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate()
        List> converterList = restTemplate.getMessageConverters()
        for (HttpMessageConverter item : converterList) {
            if (item.getClass() == StringHttpMessageConverter.class) {
                item = new StringHttpMessageConverter(StandardCharsets.UTF_8)
            }
            if (item.getClass() == MappingJackson2HttpMessageConverter.class) {
                MappingJackson2HttpMessageConverter converter = item as MappingJackson2HttpMessageConverter
                def mapper = converter.getObjectMapper()
                mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
            }
        }
        return restTemplate
    }
}

你可能感兴趣的:(ObjectMapper默认Number转换为Double解决方式)