Commit 09872bd0b04348e253de493df15beedbe82afccb

Authored by zhangdaiscott
1 parent 1b75a5c0

Long转json精度丢失的配置 未生效

jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java
1 1 package org.jeecg.config;
2 2  
  3 +import com.fasterxml.jackson.databind.DeserializationFeature;
3 4 import com.fasterxml.jackson.databind.ObjectMapper;
4 5 import com.fasterxml.jackson.databind.module.SimpleModule;
5 6 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
6 7 import org.springframework.beans.factory.annotation.Value;
7 8 import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
  9 +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8 10 import org.springframework.context.annotation.Bean;
9 11 import org.springframework.context.annotation.Conditional;
10 12 import org.springframework.context.annotation.Configuration;
11   -import org.springframework.http.converter.HttpMessageConverter;
12   -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  13 +import org.springframework.context.annotation.Primary;
  14 +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
13 15 import org.springframework.web.cors.CorsConfiguration;
14 16 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
15 17 import org.springframework.web.filter.CorsFilter;
... ... @@ -17,8 +19,6 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
17 19 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
18 20 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
19 21  
20   -import java.util.List;
21   -
22 22 /**
23 23 * Spring Boot 2.0 解决跨域问题
24 24 *
... ... @@ -71,20 +71,24 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
71 71 return new CorsFilter(urlBasedCorsConfigurationSource);
72 72 }
73 73  
  74 +
74 75 /**
75   - * 添加Long转json精度丢失的配置
76   - * @Return: void
77   - */
78   - @Override
79   - public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
80   - MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
  76 + * 序列换成json时,将所有的long变成string
  77 + * js中long过长精度丢失
  78 + */
  79 + @Bean
  80 + @Primary
  81 + @ConditionalOnMissingBean(ObjectMapper.class)
  82 + public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
81 83 ObjectMapper objectMapper = new ObjectMapper();
82 84 SimpleModule simpleModule = new SimpleModule();
  85 + //忽略在json字符串中存在,在java类中不存在字段,防止错误。
  86 + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  87 + objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
83 88 simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
84 89 simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
85 90 objectMapper.registerModule(simpleModule);
86   - jackson2HttpMessageConverter.setObjectMapper(objectMapper);
87   - converters.add(jackson2HttpMessageConverter);
  91 + return objectMapper;
88 92 }
89 93  
90 94 /**
... ...