diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java index 5f62d7e2b9f..67addf8f16a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java @@ -101,7 +101,11 @@ class DefaultServerRequest implements ServerRequest { this.params = CollectionUtils.toMultiValueMap(new ServletParametersMap(servletRequest)); this.attributes = new ServletAttributesMap(servletRequest); - this.requestPath = ServletRequestPathUtils.getParsedRequestPath(servletRequest); + // DispatcherServlet parses the path but for other scenarios (e.g. tests) we might need to + + this.requestPath = (ServletRequestPathUtils.hasParsedRequestPath(servletRequest) ? + ServletRequestPathUtils.getParsedRequestPath(servletRequest) : + ServletRequestPathUtils.parseAndCache(servletRequest)); } private static List allSupportedMediaTypes(List> messageConverters) {