前言:
Spring Boot 2.6.x 版本引入依赖 springfox-boot-starter (Swagger 3.0) 后,启动项目会报错:
Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…
原因:
解决方案
import
org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
* software:IntelliJ IDEA 2022.1
* class name: WebMvcConfig
* class description: web 配置类
* @author MoBaiJun 2022/5/13 11:25
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
* 解决 org.springframework.context.ApplicationContextException: Failed to start bean <br>
* 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
* 发现如果继承了 WebMvcConfigurationSupport,则在 yml 中配置的相关内容会失效。 需要重新指定静态资源
* @param registry ResourceHandlerRegistry
* @see <a href="https://www.mobaijun.com/posts/3051425539.html"></a>
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html", "doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
PS Time 2022:05:20 : 使用上面这种引发了一个新问题,那就是 swagger-ui/index.html 无法访问了 , 以下为解决方案
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
* software:IntelliJ IDEA 2022.1
* class name: WebMvcConfig
* class description: web 配置类
* @author MoBaiJun 2022/5/13 11:25
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
* 解决 org.springframework.context.ApplicationContextException: Failed to start bean <br>
* 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
* <a href="http://localhost:8891/swagger-ui/index.html#/">...</a>
* @param registry ResourceHandlerRegistry
* @see <a href="https://www.mobaijun.com/posts/3051425539.html"></a>
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
// 解决 swagger 无法访问
registry.addResourceHandler("swagger-ui/**", "doc.html")
// 解决 doc.html 无法访问
.addResourceLocations("classpath:/META-INF/resources/**",
// 解决 swagger 无法访问
"classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
// 解决 swagger js 无法访问
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
spring
:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
需要注意的是:这种方法无法彻底解决问题,只有在不使用 Spring Boot 的执行器时,此功能才起作用。
无论配置的匹配策略如何,执行器将始终使用基于路径模式的解析 (也就是默认策略) 。
如果你想在 Spring Boot 2.6 及更高版本中将其与执行器一起使用,则需要对 Springfox 进行更改。
在你的项目里添加这个 bean :(加在配置类里就可) Spring Boot 2.6.x 整合 Swagger 启动失败报错问题解决(治标还治本)_toollong 的博客 -CSDN 博客
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
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);