Springboot2.6.x高版本与Swagger2版本冲突问题怎么解决
作者:小确幸
时间:2023-05-13
浏览:1
问题:SpringBoot2.6.x版本引入依赖springfox-boot-starter(Swagger3.0)后,启动容器会报错:Failedtostartbean‘documentationPluginsBootstrapper‘;nestedexception…原因Springfox假设SpringMVC的路径匹配策略是ant-path-matcher,而SpringBoot2.6.x版本的默认匹配策略是path-pattern-matcher,这就造成了上
问题:
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;
@Component
public 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;
@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

作者最新文章
Photoshop抠图教程详细步骤图解:新手入门常用方法与技巧
2026-09-22 14:38
Windows 10
2026-09-16 17:44
Python安装后怎么打开:使用IDLE或命令行启动解释器
2026-09-16 13:54
Windows系统Python安装教程:下载、勾选PATH及环境变量配置
2026-09-16 13:53
“等灯不计时”落地解析:算法善意如何转化为技术能力与生态协同
2026-09-08 18:03
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多


































