Springboot2.6.x高版本与Swagger2版本冲突问题怎么解决
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,今天小编给大家分享一下Springboot2.6.x高版本与Swagger2版本冲突问题怎么解决的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希
千家信息网最后更新 2025年11月08日Springboot2.6.x高版本与Swagger2版本冲突问题怎么解决
今天小编给大家分享一下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配置
org.springframework.boot spring-boot-starter-web 2.6.4 io.springfox springfox-boot-starter 3.0.0
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; @Componentpublic class SwaggerBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { List handlerMappings = getHandlerMappings(bean); customizeSpringfoxHandlerMappings(handlerMappings); } return bean; } private void customizeSpringfoxHandlerMappings(List mappings) { List copy = mappings.stream() .filter(mapping -> mapping.getPatternParser() == null) .collect(Collectors.toList()); mappings.clear(); mappings.addAll(copy); } @SuppressWarnings("unchecked") private List getHandlerMappings(Object bean) { try { Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); field.setAccessible(true); return (List) 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; @Configurationpublic 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版本冲突问题怎么解决"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
版本
知识
篇文章
问题
冲突
内容
策略
面的
配置
不同
很大
原因
大部分
容器
就是
方案
更多
知识点
行业
解决方案
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
聊天软件开发的特点有哪些
微信服务器的安全性
数据库系统可分为哪几个阶段
创建数据库用户同时授予权限
海康pms系统提示连接数据库
软件开发的信息系统
数据库计算字段创建
最近国家网络安全专项整治
中国网络安全大会2022天津
高级软件开发主要课程
软件开发阶段的v模型
网络安全售后工程师
如何配置域服务器的安全
高并发时序数据库
库尔勒便宜服务器
数据库记录日志文件
忠县网络安全防范
三台服务器共用一个平台
宜兴微型计算机网络技术推广设备
网络安全有哪一年实施的
正宗网络技术加盟联系方式
idea uml 数据库
举例说明什么是网络安全主要威胁
密钥网络安全
网络安全诵读篇
数据库 会话配置
宁波天际盛合互联网科技
数据库在计算机领域的应用
织梦数据库转别的数据库
新炬网络技术 上市