Spring Framework
Spring MVC
Java
NullPointerException
Debugging

Cannot invoke org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns because this.condition is null

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null is a NullPointerException that occurs in Spring MVC when the request mapping infrastructure encounters a handler method whose URL pattern condition has not been properly initialized. This is most commonly caused by version incompatibilities between Spring Boot, Spring Framework, and third-party libraries like Springfox/Swagger or Spring Cloud.

Common Causes

1. Springfox Swagger Incompatibility (Most Common)

The most frequent cause is using Springfox (Swagger 2.x) with Spring Boot 2.6+ or Spring Boot 3.x. Spring Boot 2.6 changed the default path matching strategy from AntPathMatcher to PathPatternParser, which Springfox does not support:

properties
1# Spring Boot 2.6+ default:
2spring.mvc.pathmatch.matching-strategy=path_pattern_parser
3
4# Springfox requires:
5spring.mvc.pathmatch.matching-strategy=ant_path_matcher

Quick Fix: Add to application.properties:

properties
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

Better Fix: Migrate from Springfox to SpringDoc OpenAPI, which supports the new path matching:

xml
1<!-- Remove Springfox -->
2<!-- <dependency>
3    <groupId>io.springfox</groupId>
4    <artifactId>springfox-boot-starter</artifactId>
5</dependency> -->
6
7<!-- Add SpringDoc -->
8<dependency>
9    <groupId>org.springdoc</groupId>
10    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
11    <version>2.3.0</version>
12</dependency>

2. Spring Boot 3.x with Legacy Libraries

Spring Boot 3.x (Spring Framework 6) requires Jakarta EE instead of javax. Libraries that still use javax servlets cause initialization failures:

xml
1<!-- Ensure all dependencies are Jakarta-compatible -->
2<dependency>
3    <groupId>org.springdoc</groupId>
4    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
5    <version>2.3.0</version> <!-- 2.x for Spring Boot 3 -->
6</dependency>

3. Custom RequestMappingHandlerMapping

If you have a custom RequestMappingHandlerMapping that does not properly initialize conditions:

java
1// BAD: Not calling super properly
2@Configuration
3public class CustomConfig extends WebMvcConfigurationSupport {
4    @Override
5    protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
6        return new CustomHandlerMapping(); // May miss initialization
7    }
8}

4. Actuator Endpoint Conflicts

Spring Boot Actuator endpoints can trigger this when combined with certain path matching configurations:

properties
# Try setting actuator base path explicitly
management.endpoints.web.base-path=/actuator
management.endpoints.web.exposure.include=health,info

Step-by-Step Diagnosis

Step 1: Identify the Conflicting Library

Check your dependencies for known incompatible libraries:

bash
1# Maven
2mvn dependency:tree | grep -i "springfox\|swagger\|springdoc"
3
4# Gradle
5gradle dependencies | grep -i "springfox\|swagger\|springdoc"

Step 2: Check Spring Boot Version

bash
mvn dependency:tree | grep spring-boot
Spring BootDefault Path MatchingSpringfox Compatible?
2.5.x and belowant_path_matcherYes
2.6.x - 2.7.xpath_pattern_parserNo (without config)
3.xpath_pattern_parserNo (use SpringDoc 2.x)

Step 3: Check the Full Stack Trace

The stack trace will show which handler is causing the issue:

 
java.lang.NullPointerException: Cannot invoke "...PatternsRequestCondition.getPatterns()"
    at org.springframework.web.servlet.mvc.method.RequestMappingInfo.getPatternValues(...)
    at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(...)

If springfox appears in the trace, it confirms the Springfox incompatibility.

Migration from Springfox to SpringDoc

Remove Springfox Configuration

java
1// Delete this:
2// @EnableSwagger2
3// @Configuration
4// public class SwaggerConfig {
5//     @Bean
6//     public Docket api() { ... }
7// }

Add SpringDoc

java
1// No @Enable annotation needed — SpringDoc auto-configures
2
3// Optional customization
4@Configuration
5public class OpenApiConfig {
6    @Bean
7    public OpenAPI customOpenAPI() {
8        return new OpenAPI()
9            .info(new Info()
10                .title("My API")
11                .version("1.0"));
12    }
13}

SpringDoc serves Swagger UI at /swagger-ui.html and the OpenAPI spec at /v3/api-docs.

Common Pitfalls

  • Partial fix: Setting ant_path_matcher fixes the error but opts out of Spring Boot's improved path matching. This is a temporary workaround — migrate to SpringDoc for a permanent fix.
  • Multiple Swagger dependencies: Having both Springfox and SpringDoc on the classpath causes conflicts. Remove one completely, including transitive dependencies.
  • Spring Security interaction: If Spring Security is configured, ensure Swagger/OpenAPI paths are permitted:
java
1@Bean
2public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3    http.authorizeHttpRequests(auth -> auth
4        .requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
5        .anyRequest().authenticated()
6    );
7    return http.build();
8}
  • Spring Cloud Gateway: Spring Cloud Gateway uses a reactive stack and has its own path matching. Ensure you use springdoc-openapi-starter-webflux-ui instead of the WebMVC variant.

Summary

  • This error is most commonly caused by Springfox (Swagger 2.x) incompatibility with Spring Boot 2.6+
  • Quick fix: set spring.mvc.pathmatch.matching-strategy=ant_path_matcher
  • Permanent fix: migrate from Springfox to SpringDoc OpenAPI (springdoc-openapi-starter-webmvc-ui)
  • For Spring Boot 3.x, use SpringDoc 2.x (Jakarta EE compatible)
  • Always check the stack trace to identify which library triggers the null condition

Course illustration
Course illustration

All Rights Reserved.