How to Fix Spring Boot 2.6 Swagger NullPointerException by Switching to ant_path_matcher
After upgrading to Spring Boot 2.6, a NullPointerException occurs when integrating Swagger due to the new PathPatternParser, and the issue can be resolved by changing the path‑matching strategy to ant_path_matcher or adding a custom WebMvcEndpointHandlerMapping bean.
Recently, after upgrading the Spring Boot version, a NullPointerException appeared when integrating Swagger, which many developers initially blamed on incorrect configuration.
The error stack trace is shown below:
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 spring... (stack trace continues)
... 14 common frames omittedThe problem is discussed in a closed Spring Boot issue and an active Springfox issue, and it mainly occurs in Spring Boot 2.6 and later because the new PathPatternParser replaces the old matching mechanism.
Two common solutions have emerged from the discussion:
Switch the path‑matching strategy back to ant_path_matcher by adding the following property:
spring.mvc.pathmatch.matching-strategy=ant_path_matcherAdd a custom @Bean that defines WebMvcEndpointHandlerMapping:
@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));
}I applied the first solution by adding the property, and the issue was resolved. If you encounter the same problem, try switching the matching strategy or adding the bean as shown above.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
