|
|
@@ -0,0 +1,68 @@
|
|
|
+package com.nb.common.doc;
|
|
|
+
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
|
|
|
+import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
|
|
+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.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName : TrDocAutoConfiguration
|
|
|
+ * @Description : 解决doc和springboot版本不兼容问题
|
|
|
+ * @Author : LF
|
|
|
+ * @Date: 2023年03月21日
|
|
|
+ */
|
|
|
+@EnableSwagger2WebMvc
|
|
|
+@Import({BeanValidatorPluginsConfiguration.class})
|
|
|
+public class TrDocAutoConfiguration {
|
|
|
+ @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));
|
|
|
+ }
|
|
|
+ if(bean instanceof Docket){
|
|
|
+ Docket docket= (Docket) bean;
|
|
|
+ docket.useDefaultResponseMessages(false)
|
|
|
+ .globalResponseMessage(RequestMethod.GET, Collections.emptyList())
|
|
|
+ .globalResponseMessage(RequestMethod.POST,Collections.emptyList());
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+}
|