PathRoutePredicateFactory: matchTrailingSlash has no effect since Spring Framework 7.0
Describe the bug
In Spring Cloud Gateway 5.0.x (built on Spring Framework 7.0 / Spring Boot 4), the matchTrailingSlash configuration on the Path route predicate no longer has any effect.
The root cause is that Spring Framework 7.0 removed PathPatternParser.setMatchOptionalTrailingSeparator() as part of spring-projects/spring-framework#34036. The Spring Cloud Gateway PathRoutePredicateFactory previously relied on this API to implement trailing slash tolerance.
In the current main branch of Spring Cloud Gateway, the call is commented out with a FIXME:
// FIXME: 5.0.0 setMatchOptionalTrailingSeparator missing
// pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchTrailingSlash());
This means the Config.matchTrailingSlash property still exists and is configurable, but silently does nothing — which is a breaking behavioral change for users relying on it.
Expected Behavior
When matchTrailingSlash is set to true (either globally or per-route), a path predicate like Path=/api/users/{segment} should match both:
/api/users/1/api/users/1/
This was the documented behavior prior to Spring Cloud Gateway 5.0.
Actual Behavior
The path predicate /api/users/{segment} only matches /api/users/1. A request to /api/users/1/ returns 404 regardless of the matchTrailingSlash setting.
Versions Affected
- Spring Cloud Gateway: 5.0.1
- Spring Framework: 7.0.7
- Spring Boot: 4.0.x
Spring's Original Behavior (pre-removal)
From the Spring Framework 6.2.x source (PathPatternParser Javadoc):
"If set to true a PathPattern without a trailing slash will also match request paths with a trailing slash."
The behavior was one-directional:
- Pattern
/api/usersmatches request/api/users/✓ - Pattern
/api/users/does NOT match request/api/users✗
The matching was handled internally by individual PathElement implementations during pattern traversal via MatchingContext.isMatchOptionalTrailingSeparator().
Sample
Since PathPatternParser no longer supports this natively, Spring Cloud Gateway should implement the trailing slash tolerance at the predicate level. A possible approach:
- When
matchTrailingSlashistrueand the initialPathPattern.matches(path)fails: - If the request path ends with
/, strip it and retry the match - Always restore the original path container in the exchange to avoid leaking state to downstream filters
This preserves the original one-directional semantics (patterns without trailing slash match requests with trailing slash) without depending on removed PathPatternParser internals.
Hi, I would like to help with this issue.
From what I understand, matchTrailingSlash still exists on the Path route predicate, but it no longer has an effect in Spring Cloud Gateway 5.x because Spring Framework 7 removed PathPatternParser#setMatchOptionalTrailingSeparator(boolean).
I can start by adding a focused regression test showing that a route such as /api/users/{id} should match /api/users/1/ when matchTrailingSlash=true, and then restore the previous one-directional behavior at the Gateway predicate level.
Would a contribution in that direction be welcome?
Please wait until after the team has triaged the issue
Hi, is there any update for this issue?
We ran into this after upgrading to Spring Boot 4.1.2 / Spring Cloud 2025.1, still present in spring-cloud-gateway-server-webflux 5.0.4 with Spring Framework 7.0.10.
Workaround: list the trailing-slash variant as an explicit second pattern.