fix spring2.6以后和knife4j冲突
parent
603ebff3cd
commit
868c147511
|
@ -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 <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
|
||||
mappings.removeIf(mapping -> mapping.getPatternParser() != null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
|
||||
try {
|
||||
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
|
||||
field.setAccessible(true);
|
||||
return (List<RequestMappingInfoHandlerMapping>) 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<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
|
||||
// Collection<ExposableWebEndpoint> 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));
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue