您的位置:首页 > 游戏 > 手游 > 问题记录-SpringBoot 2.7.2 整合 Swagger 报错

问题记录-SpringBoot 2.7.2 整合 Swagger 报错

2024/12/28 3:46:53 来源:https://blog.csdn.net/qq_43259860/article/details/140599185  浏览:    关键词:问题记录-SpringBoot 2.7.2 整合 Swagger 报错

详细报错如下

报错背景,我将springboot从2.3.3升级到了2.7.2,报了下面的错误:

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerExceptionat org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.22.jar:5.3.22]at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.22.jar:5.3.22]at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.22.jar:5.3.22]at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_372]at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.22.jar:5.3.22]at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.22.jar:5.3.22]at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.22.jar:5.3.22]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.22.jar:5.3.22]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.2.jar:2.7.2]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.2.jar:2.7.2]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.2.jar:2.7.2]at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.2.jar:2.7.2]at com.yong.TemplateApplication.main(TemplateApplication.java:35) [classes/:na]
Caused by: java.lang.NullPointerException: nullat springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[na:1.8.0_372]at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_372]at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_372]at java.util.Arrays.sort(Arrays.java:1512) ~[na:1.8.0_372]at java.util.ArrayList.sort(ArrayList.java:1464) ~[na:1.8.0_372]at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) ~[na:1.8.0_372]at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_372]at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_372]at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_372]at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_372]at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483) ~[na:1.8.0_372]at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_372]at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_372]at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_372]at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566) ~[na:1.8.0_372]at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_372]at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384) ~[na:1.8.0_372]at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_372]at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_372]at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_372]at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_372]at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566) ~[na:1.8.0_372]at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107) ~[springfox-spring-web-3.0.0.jar:3.0.0]at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91) ~[springfox-spring-web-3.0.0.jar:3.0.0]at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82) ~[springfox-spring-web-3.0.0.jar:3.0.0]at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100) ~[springfox-spring-web-3.0.0.jar:3.0.0]at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.22.jar:5.3.22]... 12 common frames omittedDisconnected from the target VM, address: '127.0.0.1:63816', transport: 'socket'Process finished with exit code 1

排查:
跟踪到时这里空指针WebMvcPatternsRequestConditionWrapper#getPatterns
在这里插入图片描述
这里之所以为空是因为,这里走了PATH_PATTERN_PARSER默认走的是这个策略:
在这里插入图片描述

走这个默认策略就会设置WebMvcAutoConfiguration#configurePathMatch,然后调用setPatternParser
在这里插入图片描述

然后子build逻辑的时候,如果patternParser不为空则走上面的分支,所以PatternsRequestCondition 不会赋值,所以下面使用就报错了空指针:
在这里插入图片描述
所以我们再结合上面,的分析我们追踪到可以通过配置改变这个策略,到此位置问题解决:

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

但是为很好奇为什么springboot2.3.3就不会有这个问题呢?整合方式没有变呀,那只能去源码找答案了。你会发现你找不到MatchingStrategy,下面的代码如果不配置默认就是AntPathMatcher,到这里就明显了所以2.3.3没有报错。

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com