Spring Boot整合Swagger时PatternsRequestCondition.getPatterns()报错:"this.condition" is null
前段时间看到群里有吐槽swagger整合问题,当时没仔细看,总以为是姿势不对。
这两天正好自己升级Spring Boot版本,然后突然出现了这样的一个错误:
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null at 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.base/java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:473) ~[na: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:1307) ~[na:na] at java.base/java.util.ArrayList.sort(ArrayList.java:1721) ~[na:na] at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) ~[na:na] at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na] at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na] at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na] at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na] at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na] at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na] at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0] at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na] at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na] at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na] at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na] 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.20.jar:5.3.20] ... 14 common frames omitted
查了一下,找到一个Spring Boot下的Issue: http://github.com/spring-projects/spring-boot/issues/28794 ,但这个issue已经关闭了,目前这个问题的主要讨论在springfox,具体issue是这个: http://github.com/springfox/springfox/issues/3462
顺着这个issue看了一下,这个问题主要出现在Spring Boot 2.6及以后,只要是Spring Boot 2.6引入的新PathPatternParser导致的。
目前从评论中得处的解决方案是:
- Path匹配策略切换回
ant_path_matcher
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
- 添加下面这个Bean的定义
@Bean public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) { List<ExposableEndpoint<?>> allEndpoints = new ArrayList(); Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints(); allEndpoints.addAll(webEndpoints); allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); String basePath = webEndpointProperties.getBasePath(); EndpointMapping endpointMapping = new EndpointMapping(basePath); boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath); return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null); } private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) { return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT)); }
我这边直接用了第一种方法,加了配置就好了。如果你最近也遇到这个问题,希望这篇文章对你起到帮助!
如果您学习过程中如遇困难?可以加入我们超高质量的Spring技术交流群,参与交流与讨论,更好的学习与进步!更多 Spring Boot教程可以点击直达! ,欢迎收藏与转发支持!
「其他文章」
- 淘宝惊现GitHub“爱国套餐”,其实是刷Star
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- 重磅!Spring Cloud 生态再添新套件:Spring Cloud Tencent
- 免申请直接用上 IDEA 新 UI,只需要这三步配置
- 如何设置IntelliJ IDEA的内存和启动参数
- spring-configuration-metadata.json文件是做啥的?
- JetBrains IDE全新UI预览版来了,要做简洁与强大兼顾的IDE
- 注意:Spring Boot 2.7开始spring.factories不推荐使用了,接下来这么玩...
- Spring Boot整合Swagger时PatternsRequestCondition.getPatterns()报错:"this.condition" is null
- Spring Boot Starter配置spring.factories的自动生成神器:mica-auto
- Stream.toList()和Collectors.toList()的性能比较
- Java Stream转List的几种方法
- Java中如何快捷的创建不可变集合
- 一文学会Java的交互式编程环境jshell
- Gitee仓库开源要审核为什么被喷?
- Java 15 新特性:隐藏类
- Java 17 新特性:switch的模式匹配(Preview)
- Java 16 新特性:record类
- PyScript:让Python在HTML中运行
- Java 14中对switch的增强,终于可以不写break了