Springboot2.6.x高版本与Swagger2版本冲突问题解决方法
问题:
Spring Boot 2.6.x版本引入依赖 springfox-boot-starter
(Swagger 3.0) 后,启动容器会报错:
Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…
原因
Springfox 假设 Spring MVC 的路径匹配策略是 ant-path-matcher
,而 Spring Boot 2.6.x版本的默认匹配策略是 path-pattern-matcher
,这就造成了上面的报错。
完整解决方案:
1. pom配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2. 添加Bean
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; import org.springframework.util.ReflectionUtils; import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; import java.lang.reflect.Field; import java.util.List; import java.util.stream.Collectors; @Component public class SwaggerBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { List<RequestMappingInfoHandlerMapping> handlerMappings = getHandlerMappings(bean); customizeSpringfoxHandlerMappings(handlerMappings); } 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); } } }
3. swagger配置类继承 WebMvcConfigurationSupport
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class Swagger2Config extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(ApiInfo.DEFAULT); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); } }
4. 访问 http://{ip}:{port}/swagger-ui/index.html
当然还是要感谢技术大佬,我只会汇总解决了治本的完整解决方法
到此这篇关于Springboot2.6.x高版本与Swagger2版本冲突问题解决方法的文章就介绍到这了,更多相关Springboot2.6.x与Swagger2版本冲突内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
springMVC如何将controller中Model数据传递到jsp页面
本篇文章主要介绍了springMVC如何将controller中Model数据传递到jsp页面,具有一定的参考价值,有兴趣的可以了解一下2017-07-07springboot 集成pgsql+mybatis plus的详细步骤
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等,下面分步骤给大家介绍springboot 集成pgsql+mybatis plus的过程,感兴趣的朋友一起看看吧2023-12-12一文搞懂JMeter engine中HashTree的配置问题
本文主要介绍了JMeter engine中HashTree的配置,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-09-09详解Java多线程编程中互斥锁ReentrantLock类的用法
Java多线程并发的程序中使用互斥锁有synchronized和ReentrantLock两种方式,这里我们来详解Java多线程编程中互斥锁ReentrantLock类的用法:2016-07-07
最新评论