From a81ba68da19efc6f09fe70497f4cf5d4d6c3b190 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 1 Jul 2022 18:07:06 +0100 Subject: [PATCH] Update remaining trailingSlashMatch default value See gh-28552 --- .../mvc/method/RequestMappingInfo.java | 12 ++++++++--- .../RequestMappingHandlerMapping.java | 2 +- ...nnotationControllerHandlerMethodTests.java | 20 +++++++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java index 3adf3ca633..9b6262b512 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -876,7 +876,7 @@ public final class RequestMappingInfo implements RequestConditionBy default this is set to 'true'. + *

The default was changed in 6.0 from {@code true} to {@code false} in + * order to support the deprecation of the property. + * @deprecated as of 6.0, see + * {@link PathPatternParser#setMatchOptionalTrailingSeparator(boolean)} */ + @Deprecated public void setTrailingSlashMatch(boolean trailingSlashMatch) { this.trailingSlashMatch = trailingSlashMatch; } /** * Return whether to apply trailing slash matching in PatternsRequestCondition. + * @deprecated as of 6.0 together with {@link #setTrailingSlashMatch(boolean)} */ + @Deprecated public boolean useTrailingSlashMatch() { return this.trailingSlashMatch; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java index 1da175b48b..381a4a73d5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java @@ -82,7 +82,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi private boolean useRegisteredSuffixPatternMatch = false; - private boolean useTrailingSlashMatch = true; + private boolean useTrailingSlashMatch = false; private Map>> pathPrefixes = Collections.emptyMap(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java index d3dc1ab219..4b546a2a8f 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java @@ -178,7 +178,15 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl @PathPatternsParameterizedTest void emptyValueMapping(boolean usePathPatterns) throws Exception { - initDispatcherServlet(ControllerWithEmptyValueMapping.class, usePathPatterns); + initDispatcherServlet(ControllerWithEmptyValueMapping.class, usePathPatterns, wac -> { + if (!usePathPatterns) { + // UrlPathHelper returns "/" for "", + // so either the mapping has to be "/" or trailingSlashMatch must be on + RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class); + mappingDef.getPropertyValues().add("useTrailingSlashMatch", true); + wac.registerBeanDefinition("handlerMapping", mappingDef); + } + }); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setContextPath("/foo"); @@ -190,7 +198,15 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl @PathPatternsParameterizedTest void errorThrownFromHandlerMethod(boolean usePathPatterns) throws Exception { - initDispatcherServlet(ControllerWithErrorThrown.class, usePathPatterns); + initDispatcherServlet(ControllerWithErrorThrown.class, usePathPatterns, wac -> { + if (!usePathPatterns) { + // UrlPathHelper returns "/" for "", + // so either the mapping has to be "/" or trailingSlashMatch must be on + RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class); + mappingDef.getPropertyValues().add("useTrailingSlashMatch", true); + wac.registerBeanDefinition("handlerMapping", mappingDef); + } + }); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setContextPath("/foo");