51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

【WEB 系列】高版本 SpringBoot 整合 Swagger 启动异常问题

前言: {#前言:}

Spring Boot 2.6.x 版本引入依赖 springfox-boot-starter (Swagger 3.0) 后,启动项目会报错:

Failed to start bean ' documentationPluginsBootstrapper ' ; nested exception...

异常信息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-05-13 11:34:35.770 [main] ERROR org.springframework.boot.SpringApplication - Application run failed org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

原因: {#原因:}

Springfox 假设 Spring MVC 的路径匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x 版本的默认匹配策略是 path-pattern-matcher,这就造成了上面的报错。

解决方案 {#解决方案}

  1. 第一种,添加配置类

继承 WebMvcConfigurationSupport 重写 addResourceHandlers 方法。

  • 编写配置类
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`);`
    `}`
    `}

  1. 第二种,修改配置文件(我没有尝试过)
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

需要注意的是:这种方法无法彻底解决问题,只有在不使用 Spring Boot 的执行器时,此功能才起作用。

无论配置的匹配策略如何,执行器将始终使用基于路径模式的解析 (也就是默认策略) 。

如果你想在 Spring Boot 2.6 及更高版本中将其与执行器一起使用,则需要对 Springfox 进行更改。

这个办法是我在 github 上找到的,一个大佬提了一个解决方案是将 Springfox 的某 .java 文件复制到自己项目里进行修改,另一个大佬提了一个更好的解决方案,我觉得针不戳,在这里分享一下:

在你的项目里添加这个 bean :(加在配置类里就可)Spring Boot 2.6.x 整合 Swagger 启动失败报错问题解决(治标还治本)_toollong 的博客 -CSDN 博客

@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
    return new BeanPostProcessor() {

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">postProcessAfterInitialization</span><span class="token punctuation">(</span><span class="token class-name">Object</span> bean<span class="token punctuation">,</span> <span class="token class-name">String</span> beanName<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">BeansException</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>bean <span class="token keyword">instanceof</span> <span class="token class-name">WebMvcRequestHandlerProvider</span> <span class="token operator">||</span> bean <span class="token keyword">instanceof</span> <span class="token class-name">WebFluxRequestHandlerProvider</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">customizeSpringfoxHandlerMappings</span><span class="token punctuation">(</span><span class="token function">getHandlerMappings</span><span class="token punctuation">(</span>bean<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> bean<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">private</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span> <span class="token keyword">extends</span> <span class="token class-name">RequestMappingInfoHandlerMapping</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">void</span> <span class="token function">customizeSpringfoxHandlerMappings</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> mappings<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> copy <span class="token operator">=</span> mappings<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>mapping <span class="token operator">-&gt;</span> mapping<span class="token punctuation">.</span><span class="token function">getPatternParser</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            mappings<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            mappings<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span>copy<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token annotation punctuation">@SuppressWarnings</span><span class="token punctuation">(</span><span class="token string">"unchecked"</span><span class="token punctuation">)</span>
        <span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">RequestMappingInfoHandlerMapping</span><span class="token punctuation">&gt;</span></span> <span class="token function">getHandlerMappings</span><span class="token punctuation">(</span><span class="token class-name">Object</span> bean<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token class-name">Field</span> field <span class="token operator">=</span> <span class="token class-name">ReflectionUtils</span><span class="token punctuation">.</span><span class="token function">findField</span><span class="token punctuation">(</span>bean<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"handlerMappings"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                field<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">RequestMappingInfoHandlerMapping</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">)</span> field<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>bean<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IllegalArgumentException</span> <span class="token operator">|</span> <span class="token class-name">IllegalAccessException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">IllegalStateException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>



}

参考文章:

java - Failed to start bean 'documentationPluginsBootstrapper' in spring data rest - Stack Overflow

Spring Boot 2.6.x 整合 Swagger 启动失败报错问题解决(治标还治本)_toollong 的博客 -CSDN 博客

赞(0)
未经允许不得转载:工具盒子 » 【WEB 系列】高版本 SpringBoot 整合 Swagger 启动异常问题