From fe1b58ade2fd14ac6d01a22450f80088bd08046b Mon Sep 17 00:00:00 2001 From: zhangdaiscott Date: Sun, 26 Sep 2021 17:35:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=98=E5=8E=9Flong=E7=B2=BE=E5=BA=A6?= =?UTF-8?q?=E6=94=B9=E9=80=A0=EF=BC=8C=E5=AF=BC=E8=87=B4=E4=BA=86=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E4=B8=A5=E9=87=8D=E9=97=AE=E9=A2=98=201=E8=A1=A8?= =?UTF-8?q?=E5=8D=95=E7=9A=84=E6=97=A5=E6=9C=9F=E5=AD=97=E6=AE=B5=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E6=98=BE=E7=A4=BA=E6=97=A5=E6=9C=9F=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=202=E5=89=8D=E7=AB=AF=E5=88=86=E9=A1=B5=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/config/WebMvcConfiguration.java | 28 ++++++++----------- .../handler/MySwaggerResourceProvider.java | 14 +++++++--- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java b/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java index bf35e015..9b2689d5 100644 --- a/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java +++ b/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java @@ -1,17 +1,15 @@ package org.jeecg.config; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @@ -19,6 +17,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.util.List; + /** * Spring Boot 2.0 解决跨域问题 * @@ -71,24 +71,20 @@ public class WebMvcConfiguration implements WebMvcConfigurer { return new CorsFilter(urlBasedCorsConfigurationSource); } - /** - * 序列换成json时,将所有的long变成string - * js中long过长精度丢失 - */ - @Bean - @Primary - @ConditionalOnMissingBean(ObjectMapper.class) - public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { + * 添加Long转json精度丢失的配置 + * @Return: void + */ + @Override + public void configureMessageConverters(List> converters) { + MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); - //忽略在json字符串中存在,在java类中不存在字段,防止错误。 - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); - return objectMapper; + jackson2HttpMessageConverter.setObjectMapper(objectMapper); + converters.add(jackson2HttpMessageConverter); } /** diff --git a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/MySwaggerResourceProvider.java b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/MySwaggerResourceProvider.java index d88d86c6..102160cc 100644 --- a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/MySwaggerResourceProvider.java +++ b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/MySwaggerResourceProvider.java @@ -1,20 +1,26 @@ package org.jeecg.handler; -import lombok.extern.slf4j.Slf4j; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.gateway.route.RouteLocator; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; + +import lombok.extern.slf4j.Slf4j; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; -import java.util.*; - /** * 聚合各个服务的swagger接口 */ @Component @Slf4j +@Primary public class MySwaggerResourceProvider implements SwaggerResourcesProvider { /** * swagger2默认的url后缀 @@ -46,7 +52,7 @@ public class MySwaggerResourceProvider implements SwaggerResourcesProvider { .filter(route -> !self.equals(route.getUri().getHost())) .subscribe(route -> routeHosts.add(route.getUri().getHost())); - // 记录已经添加过的server,存在同一个应用注册了多个服务在eureka上 + // 记录已经添加过的server,存在同一个应用注册了多个服务在nacos上 Set dealed = new HashSet<>(); routeHosts.forEach(instance -> { // 拼接url