springboot整合swagger出现了org.springframework.context.ApplicationContextException: Failed to start bean

SpringBoot2.7.x整合Swagger2 时,出现

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException``

原因是因为springboot2.6.x后与swagger2 3.0.0出现冲突
解决办法:
在Swagger配置类继承WebMvcConfigurationSupport类

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport{
	......
	......
}

另外需要注意的是,继承了WebMvcConfigurationSupport类后,spring会找不到资源,可以启动测试
项目启动后,访问http://localhost:8080/swagger-ui.html
控制台有如下信息:

14:09:34 WARN  No mapping for GET /emos-wx-api/swagger-ui.html

所以还需在配置类中添加匹配静态资源的方法:

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport{
	......
	......
	
	/**
     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

现在启动时访问即可返回swagger页面

参考:
http://t.csdn.cn/BZzvi

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop' 是由于在项目启动时出现的异常。根据提供的信息,这个异常通常与Spring Boot和Swagger的版本兼容性问题有关。具体来说,当使用Spring Boot版本为2.6.x,并且Swagger版本为3.0.0以上时,可能会导致这个异常的出现。 此外,还有可能与应用程序中的其他组件或配置有关,例如数据源的初始化错误,或者可能需要添加额外的配置,如配置spring.mvc.pathmatch.matching-strategy为ant_path_matcher[3]。 要解决这个异常,可以尝试以下几个步骤: 1. 首先,确保你的Spring Boot和Swagger的版本兼容。如果你的Spring Boot版本是2.6.x,并且Swagger版本是3.0.0以上,请确认它们之间的兼容性。 2. 检查应用程序中的其他组件或配置是否正确初始化。特别是,检查数据源的配置是否正确,并且没有引发BeanCreationException错误。 3. 如果使用了ant_path_matcher作为路径匹配策略,请确保已正确添加了相关的配置,并且没有拼写错误或其他配置问题。 通过仔细检查和排除这些可能的原因,你应该能够解决 org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop' 这个异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [解决“Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang....](https://blog.csdn.net/cxclll/article/details/126745903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [struts2驱动包](https://download.csdn.net/download/zengzitao823/2480227)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值