0


springboot及swagger2整合报错:documentationPluginsBootstrapper

springboot及swagger2整合报错:documentationPluginsBootstrapper

1. 先给出解决方案

如果英文好直接参考这篇文章:https://fullstackcode.dev/2022/06/12/swagger-is-not-working-with-spring-boot-2-6-x/,这是我尝试了网上多种解决方案无果之后,真正解决了问题的文章,当然作者参考的也是官方讨论组,详情过程可以查看:https://github.com/springfox/springfox/issues/3462。这里面也告诉我们一般出现插件问题,记得第一时间去查看开源项目里面的issues,一般你遇到的问题,大家都会遇到,尤其国外程序员一般都乐意分享,大多数时候都能找到答案。

我使用的是作者给到的第三种方案:

  1. 添加actuator依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
  1. 修改Application.yml配置:
mvc:pathmatch:matching-strategy: ANT_PATH_MATCHER

如果是properties文件,配置如下:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

swagger配置文件添加关键方法:

@Configuration@EnableSwagger2publicclassSwaggerConfig{@BeanpublicDocketapi(){returnnewDocket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}@BeanpublicWebMvcEndpointHandlerMappingwebEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes,CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties,Environment environment){List<ExposableEndpoint<?>> allEndpoints =newArrayList();Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath = webEndpointProperties.getBasePath();EndpointMapping endpointMapping =newEndpointMapping(basePath);boolean shouldRegisterLinksMapping =this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);returnnewWebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(),newEndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping,null);}privatebooleanshouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties,Environment environment,String basePath){return webEndpointProperties.getDiscovery().isEnabled()&&(StringUtils.hasText(basePath)||ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}}

从代码来看,里面的核心方法webEndpointServletHandlerMapping,做了高版本的兼容,我估计在后面Swagger如果做更新的时候,应该会做一定的兼容性。

2.高版本整合出错及详细解决方案

以前使用Swagger版本的时候,都是一次性启动成功,但是这次将Springboot使用新版本之后,居然报错了,主要的错误信息如下:

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.23.jar:5.3.23]
    at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.4.jar:2.7.4]
    at stu.up2uper.springstu.SpringStuApplication.main(SpringStuApplication.java:14) ~[classes/:na]
Caused by: java.lang.NullPointerException: null
    at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
    at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
    at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
    at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:na]
    at java.base/java.util.TimSort.sort(TimSort.java:220) ~[na:na]
    at java.base/java.util.Arrays.sort(Arrays.java:1232) ~[na:na]
    at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~[guava-20.0.jar:na]
    at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
    at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]
    at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
    at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
    at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~[guava-20.0.jar:na]
    at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~[guava-20.0.jar:na]
    at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~[guava-20.0.jar:na]
    at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~[guava-20.0.jar:na]
    at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~[guava-20.0.jar:na]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.23.jar:5.3.23]... 14 common frames omitted

我引用的相关包信息如下:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.4</version><relativePath/><!-- lookup parent from repository --></parent><dependencies><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency></dependencies>

第一反应就是版本冲突不兼容导致的,百度了几种方案都无果,主要一下几种方案:

  • 版本兼容法:降低springboot版本,使用配套swagger
  • 更新Google Guava法:升级或者指定特定guava版本
  • 修改注释法:添加@EnableWebMvc之类的注解之类来修改
@EnableAsync@EnableWebMvc@ServletComponentScan@ComponentScan(basePackages ={"com.xmair.restapi","com.xmair.core.service"})@MapperScan("com.xmair.core.mapper")publicclassMFISRestAPIApplication{publicstaticvoidmain(String[] args){ConfigurableApplicationContext context =SpringApplication.run(MFISRestAPIApplication.class, args);}}
  • 添加资源法:估计是无法获取swagger所需资源,而导致无法启动
@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");}

反正以上方案都没解决我的问题,可能上面方法在某些环境及版本之下是适用的,最终还是前面提到的方案解决了问题。方案三已在上方表述,下方把另外两种方案搬砖过来,给大家做参考:

方案一、修改配置及添加兼容代码
修改配置就是上面一样:

mvc:pathmatch:matching-strategy: ANT_PATH_MATCHER

如果是properties文件,配置如下:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

同时,修改核心代码:

@Configuration@EnableSwagger2publicclassSwaggerConfig{@BeanpublicDocketapi(){returnnewDocket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}@BeanpublicstaticBeanPostProcessorspringfoxHandlerProviderBeanPostProcessor(){returnnewBeanPostProcessor(){@OverridepublicObjectpostProcessAfterInitialization(Object bean,String beanName)throwsBeansException{if(bean instanceofWebMvcRequestHandlerProvider|| bean instanceofWebFluxRequestHandlerProvider){customizeSpringfoxHandlerMappings(getHandlerMappings(bean));}return bean;}private<TextendsRequestMappingInfoHandlerMapping>voidcustomizeSpringfoxHandlerMappings(List<T> mappings){List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser()==null).collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);}@SuppressWarnings("unchecked")privateList<RequestMappingInfoHandlerMapping>getHandlerMappings(Object bean){try{Field field =ReflectionUtils.findField(bean.getClass(),"handlerMappings");
                    field.setAccessible(true);return(List<RequestMappingInfoHandlerMapping>) field.get(bean);}catch(IllegalArgumentException|IllegalAccessException e){thrownewIllegalStateException(e);}}};}}

方案二、修改swagger配置核心代码

@Configuration@EnableSwagger2publicclassSwaggerConfig{@BeanpublicDocketapi(){returnnewDocket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}@BeanpublicInitializingBeanremoveSpringfoxHandlerProvider(DocumentationPluginsBootstrapper bootstrapper){return()-> bootstrapper.getHandlerProviders().removeIf(WebMvcRequestHandlerProvider.class::isInstance);}@BeanpublicRequestHandlerProvidercustomRequestHandlerProvider(Optional<ServletContext> servletContext,HandlerMethodResolver methodResolver,List<RequestMappingInfoHandlerMapping> handlerMappings){String contextPath = servletContext.map(ServletContext::getContextPath).orElse(Paths.ROOT);return()-> handlerMappings.stream().filter(mapping ->!mapping.getClass().getSimpleName().equals("IntegrationRequestMappingHandlerMapping")).map(mapping -> mapping.getHandlerMethods().entrySet()).flatMap(Set::stream).map(entry ->newWebMvcRequestHandler(contextPath, methodResolver,tweakInfo(entry.getKey()), entry.getValue())).sorted(byPatternsCondition()).collect(toList());}RequestMappingInfotweakInfo(RequestMappingInfo info){if(info.getPathPatternsCondition()==null)return info;String[] patterns = info.getPathPatternsCondition().getPatternValues().toArray(String[]::new);return info.mutate().options(newRequestMappingInfo.BuilderConfiguration()).paths(patterns).build();}}

希望以上的方案能够帮助大家解决问题,同时通过这次解决问题,又一次验证了遇到开源项目错误的解决途径:

简单百度无果,一定要用Google,或者开源项目的讨论区。

标签: spring boot spring java

本文转载自: https://blog.csdn.net/hunkxia/article/details/127404964
版权归原作者 石中雀 所有, 如有侵权,请联系我们删除。

“springboot及swagger2整合报错:documentationPluginsBootstrapper”的评论:

还没有评论