Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How to Fix Spring Boot 2.6 Swagger NullPointerException by Switching to ant_path_matcher

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 omitted

The 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_matcher

Add 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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
BackendJavaSpring BootNullPointerExceptionSwaggerPathPatternParser

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion