|
@@ -6,12 +6,17 @@ import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrap
|
|
|
import com.google.common.base.Predicates;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+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.lang.NonNull;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
+import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
|
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
import springfox.documentation.builders.PathSelectors;
|
|
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
@@ -20,9 +25,12 @@ import springfox.documentation.service.ApiInfo;
|
|
|
import springfox.documentation.service.ResponseMessage;
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
+import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Swagger2
|
|
@@ -81,4 +89,41 @@ public class SwaggerBootstrapConfig implements WebMvcConfigurer {
|
|
|
registry.addResourceHandler("doc.html")
|
|
|
.addResourceLocations("classpath:/META-INF/resources/");
|
|
|
}
|
|
|
+
|
|
|
+ // NOTE 解决springfox与springboot新版本不兼容问题
|
|
|
+ @Bean
|
|
|
+ public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
|
|
|
+ return new BeanPostProcessor() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
|
|
|
+ if (bean instanceof WebMvcRequestHandlerProvider ) {
|
|
|
+ customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
|
|
|
+ List<T> copy = mappings.stream()
|
|
|
+ .filter(mapping -> mapping.getPatternParser() == null)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ mappings.clear();
|
|
|
+ mappings.addAll(copy);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
|
|
|
+ try {
|
|
|
+ Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
|
|
|
+ assert field != null;
|
|
|
+ field.setAccessible(true);
|
|
|
+ return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
|
|
|
+ } catch (IllegalArgumentException | IllegalAccessException e) {
|
|
|
+ throw new IllegalStateException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|