From 868c147511cd14b4884e38c229aa076cadf21d03 Mon Sep 17 00:00:00 2001 From: jay <75509151@qq.com> Date: Fri, 2 Jun 2023 11:05:09 +0800 Subject: [PATCH] =?UTF-8?q?fix=20spring2.6=E4=BB=A5=E5=90=8E=E5=92=8Cknife?= =?UTF-8?q?4j=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iotkit/swagger/config/SwaggerConfig.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java index 7cfea208..0b09af70 100644 --- a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java +++ b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java @@ -2,16 +2,25 @@ package cc.iotkit.swagger.config; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; +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.*; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; + +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -32,6 +41,7 @@ public class SwaggerConfig { public Docket defaultApi2() { return new Docket(DocumentationType.SWAGGER_2) .groupName(applicationName) + .enable(true) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) @@ -45,4 +55,80 @@ public class SwaggerConfig { .description("Swagger API Doc") .build(); } + + // 解决springboot升级到2.6.x之后,knife4j报错 + @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) { + if (bean instanceof WebMvcRequestHandlerProvider ) { + customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); + } + return bean; + } + + private void customizeSpringfoxHandlerMappings(List mappings) { + mappings.removeIf(mapping -> mapping.getPatternParser() != null); + } + + @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); + } + } + }; + } + +// /** +// * 解决springboot升级到2.6.x之后,knife4j报错 +// * +// * @param webEndpointsSupplier the web endpoints supplier +// * @param servletEndpointsSupplier the servlet endpoints supplier +// * @param controllerEndpointsSupplier the controller endpoints supplier +// * @param endpointMediaTypes the endpoint media types +// * @param corsProperties the cors properties +// * @param webEndpointProperties the web endpoint properties +// * @param environment the environment +// * @return the web mvc endpoint handler mapping +// */ +// @Bean +// public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping( +// WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, +// ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, +// CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, +// Environment environment) { +// List> allEndpoints = new ArrayList<>(); +// Collection webEndpoints = webEndpointsSupplier.getEndpoints(); +// allEndpoints.addAll(webEndpoints); +// allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); +// allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); +// String basePath = webEndpointProperties.getBasePath(); +// EndpointMapping endpointMapping = new EndpointMapping(basePath); +// boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties, +// environment, basePath); +// return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, +// corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), +// shouldRegisterLinksMapping, null); +// } +// +// /** +// * shouldRegisterLinksMapping +// * @param webEndpointProperties webEndpointProperties +// * @param environment environment +// * @param basePath / +// * @return boolean +// */ +// private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, +// Environment environment, String basePath) { +// return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) +// || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT)); +// } }