diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java b/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java
index f7122a53184..c7be4075ac1 100644
--- a/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java
+++ b/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.server.PathContainer;
+import org.springframework.util.StringUtils;
/**
* Parser for URI path patterns producing {@link PathPattern} instances that can
@@ -103,6 +104,17 @@ public class PathPatternParser {
}
+ /**
+ * Prepare the given pattern for use in matching to full URL paths.
+ *
By default, prepend a leading slash if needed for non-empty patterns.
+ * @param pattern the pattern to initialize
+ * @return the updated pattern
+ * @since 5.2.25
+ */
+ public String initFullPathPattern(String pattern) {
+ return (StringUtils.hasLength(pattern) && !pattern.startsWith("/") ? "/" + pattern : pattern);
+ }
+
/**
* Process the path pattern content, a character at a time, breaking it into
* path elements around separator boundaries and verifying the structure at each
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 006c8fea462..b587686a675 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -109,10 +109,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(PathPatternParser.defaultInstance).apply(pattern);
+ PathPatternParser parser = PathPatternParser.defaultInstance;
+ pattern = parser.initFullPathPattern(pattern);
+ return pathPredicates(parser).apply(pattern);
}
/**
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java
index 10296b8d4b8..9c7a6bf8cbd 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,9 +28,9 @@ import org.springframework.beans.BeansException;
import org.springframework.http.server.PathContainer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
-import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
+import org.springframework.web.util.pattern.PathPatternParser;
/**
* Abstract base class for URL-mapped
@@ -185,8 +185,9 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
Object resolvedHandler = handler;
// Parse path pattern
- urlPath = prependLeadingSlash(urlPath);
- PathPattern pattern = getPathPatternParser().parse(urlPath);
+ PathPatternParser parser = getPathPatternParser();
+ urlPath = parser.initFullPathPattern(urlPath);
+ PathPattern pattern = parser.parse(urlPath);
if (this.handlerMap.containsKey(pattern)) {
Object existingHandler = this.handlerMap.get(pattern);
if (existingHandler != null && existingHandler != resolvedHandler) {
@@ -215,14 +216,4 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
}
-
- private static String prependLeadingSlash(String pattern) {
- if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
- return "/" + pattern;
- }
- else {
- return pattern;
- }
- }
-
}
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java
index b78d8532008..211b5da503e 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2021 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,7 +35,6 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
-import org.springframework.util.StringUtils;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
@@ -86,8 +85,9 @@ public class ResourceUrlProvider implements ApplicationListener handlerMap) {
this.handlerMap.clear();
handlerMap.forEach((rawPattern, resourceWebHandler) -> {
- rawPattern = prependLeadingSlash(rawPattern);
- PathPattern pattern = PathPatternParser.defaultInstance.parse(rawPattern);
+ PathPatternParser parser = PathPatternParser.defaultInstance;
+ rawPattern = parser.initFullPathPattern(rawPattern);
+ PathPattern pattern = parser.parse(rawPattern);
this.handlerMap.put(pattern, resourceWebHandler);
});
}
@@ -173,14 +173,4 @@ public class ResourceUrlProvider implements ApplicationListener result = new ArrayList<>(patterns.length);
- for (String path : patterns) {
- if (StringUtils.hasText(path) && !path.startsWith("/")) {
- path = "/" + path;
- }
- result.add(parser.parse(path));
+ for (String pattern : patterns) {
+ pattern = parser.initFullPathPattern(pattern);
+ result.add(parser.parse(pattern));
}
return result;
}
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 342074a8164..3f73e59bd08 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -107,10 +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(PathPatternParser.defaultInstance).apply(pattern);
+ PathPatternParser parser = PathPatternParser.defaultInstance;
+ pattern = parser.initFullPathPattern(pattern);
+ return pathPredicates(parser).apply(pattern);
}
/**
@@ -333,14 +332,14 @@ public abstract class RequestPredicates {
void method(Set methods);
/**
- * Receive notification of an path predicate.
+ * Receive notification of a path predicate.
* @param pattern the path pattern that makes up the predicate
* @see RequestPredicates#path(String)
*/
void path(String pattern);
/**
- * Receive notification of an path extension predicate.
+ * Receive notification of a path extension predicate.
* @param extension the path extension that makes up the predicate
* @see RequestPredicates#pathExtension(String)
*/
@@ -426,11 +425,11 @@ public abstract class RequestPredicates {
void unknown(RequestPredicate predicate);
}
+
private static class HttpMethodPredicate implements RequestPredicate {
private final Set httpMethods;
-
public HttpMethodPredicate(HttpMethod httpMethod) {
Assert.notNull(httpMethod, "HttpMethod must not be null");
this.httpMethods = EnumSet.of(httpMethod);
@@ -641,12 +640,14 @@ public abstract class RequestPredicates {
}
}
+
private static class PathExtensionPredicate implements RequestPredicate {
private final Predicate extensionPredicate;
@Nullable
private final String extension;
+
public PathExtensionPredicate(Predicate extensionPredicate) {
Assert.notNull(extensionPredicate, "Predicate must not be null");
this.extensionPredicate = extensionPredicate;
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java
index 03ae39abdfb..9421de0d80c 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2021 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java
index 4062a053405..8d23ab7b8de 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UrlPathHelper;
+import org.springframework.web.util.pattern.PathPatternParser;
/**
* A logical disjunction (' || ') request condition that matches a request
@@ -142,9 +143,7 @@ public class PatternsRequestCondition extends AbstractRequestCondition result = new LinkedHashSet<>(patterns.length);
for (String pattern : patterns) {
- if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
- pattern = "/" + pattern;
- }
+ pattern = PathPatternParser.defaultInstance.initFullPathPattern(pattern);
result.add(pattern);
}
return result;
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
index a437295c923..efffafe8658 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2021 the original author or authors.
+ * Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,6 +65,7 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
+import org.springframework.web.util.pattern.PathPatternParser;
/**
* Creates instances of {@link org.springframework.web.util.UriComponentsBuilder}
@@ -545,9 +546,7 @@ public class MvcUriComponentsBuilder {
String typePath = getClassMapping(controllerType);
String methodPath = getMethodMapping(method);
String path = pathMatcher.combine(typePath, methodPath);
- if (StringUtils.hasLength(path) && !path.startsWith("/")) {
- path = "/" + path;
- }
+ path = PathPatternParser.defaultInstance.initFullPathPattern(path);
builder.path(path);
return applyContributors(builder, method, args);