diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index a705e0e4b4e..f098c57a6cf 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -111,6 +111,9 @@ public abstract class RequestPredicates { */ public static RequestPredicate path(String pattern) { Assert.notNull(pattern, "'pattern' must not be null"); + if (!pattern.isEmpty() && !pattern.startsWith("/")) { + pattern = "/" + pattern; + } return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java index 39ff4b60dbd..b2fe7584044 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java @@ -52,7 +52,7 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati return nest(path("/foo/"), route(GET("/bar"), nestedHandler::pattern) .andRoute(GET("/baz"), nestedHandler::pattern)) - .andNest(GET("/{foo}"), + .andNest(GET("{foo}"), route(GET("/bar"), nestedHandler::variables).and( nest(GET("/{bar}"), route(GET("/{baz}"), nestedHandler::variables)))) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java index cb9bfe24aa8..85766041129 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java @@ -109,6 +109,14 @@ public class RequestPredicatesTests { assertFalse(predicate.test(request)); } + @Test + public void pathNoLeadingSlash() { + URI uri = URI.create("http://localhost/path"); + RequestPredicate predicate = RequestPredicates.path("p*"); + MockServerRequest request = MockServerRequest.builder().uri(uri).build(); + assertTrue(predicate.test(request)); + } + @Test public void pathEncoded() { URI uri = URI.create("http://localhost/foo%20bar"); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java index 57d42f45d33..672da39e29f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java @@ -107,6 +107,9 @@ public abstract class RequestPredicates { */ public static RequestPredicate path(String pattern) { Assert.notNull(pattern, "'pattern' must not be null"); + if (!pattern.isEmpty() && !pattern.startsWith("/")) { + pattern = "/" + pattern; + } return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java index b1e434d4555..f1fa58fe5a0 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java @@ -117,6 +117,14 @@ public class RequestPredicatesTests { assertFalse(predicate.test(request)); } + @Test + public void pathNoLeadingSlash() { + MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/path"); + ServerRequest request = new DefaultServerRequest(servletRequest, emptyList()); + RequestPredicate predicate = RequestPredicates.path("p*"); + assertTrue(predicate.test(request)); + } + @Test public void pathEncoded() { MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo%20bar");