|
|
@@ -1,9 +1,13 @@
|
|
|
package com.sqx.config;
|
|
|
|
|
|
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.util.ReflectionUtils;
|
|
|
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;
|
|
|
@@ -11,9 +15,12 @@ import springfox.documentation.service.ApiInfo;
|
|
|
import springfox.documentation.service.ApiKey;
|
|
|
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;
|
|
|
|
|
|
import static com.google.common.collect.Lists.newArrayList;
|
|
|
|
|
|
@@ -50,4 +57,41 @@ public class SwaggerConfig implements WebMvcConfigurer {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 解决SpringBoot和Swagger2冲突
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
|
|
|
+ return new BeanPostProcessor() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, 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");
|
|
|
+ field.setAccessible(true);
|
|
|
+ return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
|
|
|
+ } catch (IllegalArgumentException | IllegalAccessException e) {
|
|
|
+ throw new IllegalStateException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|