From 8159ffc50974d5dae3260178b1b11b59f58bcb54 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Mon, 4 Jul 2022 09:54:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:wrench:=20=E6=96=B0=E5=A2=9Eswagger?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=AE=A9swagger=E5=92=8Cactuator=E6=97=A0?= =?UTF-8?q?=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cnbm/config/SwaggerConfig.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java b/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java index ad3a255..94ac7bd 100644 --- a/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java +++ b/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java @@ -3,9 +3,14 @@ package com.cnbm.config; import com.cnbm.common.constant.Constant; import com.google.common.collect.Lists; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; +import org.springframework.util.ReflectionUtils; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @@ -14,9 +19,13 @@ import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; +import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; +import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import static com.google.common.collect.Lists.newArrayList; @@ -28,6 +37,7 @@ import static com.google.common.collect.Lists.newArrayList; @Configuration @EnableOpenApi @Profile("dev") +@EnableWebMvc public class SwaggerConfig { /** @@ -140,4 +150,37 @@ public class SwaggerConfig { new SecurityReference(Constant.TOKEN_HEADER, authorizationScopes)); } + @Bean + public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { + return new BeanPostProcessor() { + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { + customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); + } + return bean; + } + + private void customizeSpringfoxHandlerMappings(List mappings) { + List copy = mappings.stream() + .filter(mapping -> mapping.getPatternParser() == null) + .collect(Collectors.toList()); + mappings.clear(); + mappings.addAll(copy); + } + + @SuppressWarnings("unchecked") + private List getHandlerMappings(Object bean) { + try { + Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); + field.setAccessible(true); + return (List) field.get(bean); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + }; + } + }