Skip to content

Commit 14fd260

Browse files
committed
Deprecate UrlPathHelper shortcuts on AbstractHandlerMapping
Given the availability of two alternatives mechanisms for URL path matching, PathPatternParser and AntPathMatcher, and now that parsed patterns are enabled by default, it makes sense to reduce the proliferation of options on AbstractHandlerMapping by deprecating shortcuts related to String path matching. Most applications rely on Boot and on the MVC config to do all this. See gh-28607
1 parent 8d08b1d commit 14fd260

File tree

11 files changed

+44
-34
lines changed

11 files changed

+44
-34
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
5959
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
6060
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
61-
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
6261
import org.springframework.web.servlet.handler.MappedInterceptor;
6362
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
6463
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
@@ -68,6 +67,7 @@
6867
import org.springframework.web.servlet.theme.FixedThemeResolver;
6968
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
7069
import org.springframework.web.servlet.view.InternalResourceViewResolver;
70+
import org.springframework.web.util.UrlPathHelper;
7171
import org.springframework.web.util.pattern.PathPatternParser;
7272

7373
/**
@@ -353,7 +353,7 @@ public StandaloneMockMvcBuilder setUseTrailingSlashPatternMatch(boolean useTrail
353353
/**
354354
* Set if ";" (semicolon) content should be stripped from the request URI. The value,
355355
* if provided, is in turn set on
356-
* {@link AbstractHandlerMapping#setRemoveSemicolonContent(boolean)}.
356+
* {@link org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)}.
357357
*/
358358
public StandaloneMockMvcBuilder setRemoveSemicolonContent(boolean removeSemicolonContent) {
359359
this.removeSemicolonContent = removeSemicolonContent;
@@ -477,7 +477,9 @@ public RequestMappingHandlerMapping getHandlerMapping(
477477
handlerMapping.setPatternParser(null);
478478
handlerMapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
479479
if (removeSemicolonContent != null) {
480-
handlerMapping.setRemoveSemicolonContent(removeSemicolonContent);
480+
UrlPathHelper pathHelper = new UrlPathHelper();
481+
pathHelper.setRemoveSemicolonContent(removeSemicolonContent);
482+
handlerMapping.setUrlPathHelper(pathHelper);
481483
}
482484
}
483485
else if (patternParser != null) {

spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/EncodedUriTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4747
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
4848
import org.springframework.web.util.UriComponentsBuilder;
49+
import org.springframework.web.util.UrlPathHelper;
4950

5051
import static org.hamcrest.core.Is.is;
5152
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -119,7 +120,9 @@ private static class HandlerMappingConfigurer implements BeanPostProcessor, Prio
119120
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
120121
if (bean instanceof RequestMappingHandlerMapping requestMappingHandlerMapping) {
121122
// URL decode after request mapping, not before.
122-
requestMappingHandlerMapping.setUrlDecode(false);
123+
UrlPathHelper pathHelper = new UrlPathHelper();
124+
pathHelper.setUrlDecode(false);
125+
requestMappingHandlerMapping.setUrlPathHelper(pathHelper);
123126
}
124127
return bean;
125128
}

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ public PathPatternParser getPatternParser() {
179179
* <p><strong>Note:</strong> This property is mutually exclusive with and
180180
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
181181
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath(boolean)
182+
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
182183
*/
183-
@SuppressWarnings("deprecation")
184+
@Deprecated
184185
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
185186
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
186187
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
@@ -193,8 +194,9 @@ public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
193194
* <p><strong>Note:</strong> This property is mutually exclusive with and
194195
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
195196
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode(boolean)
197+
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
196198
*/
197-
@SuppressWarnings("deprecation")
199+
@Deprecated
198200
public void setUrlDecode(boolean urlDecode) {
199201
this.urlPathHelper.setUrlDecode(urlDecode);
200202
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
@@ -207,8 +209,9 @@ public void setUrlDecode(boolean urlDecode) {
207209
* <p><strong>Note:</strong> This property is mutually exclusive with and
208210
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
209211
* @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
212+
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
210213
*/
211-
@SuppressWarnings("deprecation")
214+
@Deprecated
212215
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
213216
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
214217
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMapping.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,11 +37,9 @@
3737
* a single handler.
3838
*
3939
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
40-
* matches (given "/test" -&gt; registered "/t*"). Note that the default is
41-
* to map within the current servlet mapping if applicable; see the
42-
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
43-
* For details on the pattern options, see the
44-
* {@link org.springframework.util.AntPathMatcher} javadoc.
40+
* matches (given "/test" -&gt; registered "/t*"). For details on the pattern
41+
* options, see the {@link org.springframework.web.util.pattern.PathPattern}
42+
* javadoc.
4543
*
4644
* @author Rod Johnson
4745
* @author Juergen Hoeller

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,10 +44,9 @@
4444
* with a slash, one is prepended.
4545
*
4646
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
47-
* pattern matches (given "/test" -&gt; registered "/t*"). Note that the default
48-
* is to map within the current servlet mapping if applicable; see the
49-
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property. For details on the
50-
* pattern options, see the {@link org.springframework.util.AntPathMatcher} javadoc.
47+
* matches (given "/test" -&gt; registered "/t*"). For details on the pattern
48+
* options, see the {@link org.springframework.web.util.pattern.PathPattern}
49+
* javadoc.
5150
5251
* @author Rod Johnson
5352
* @author Juergen Hoeller

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMappingTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.web.servlet.HandlerMapping;
2828
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
2929
import org.springframework.web.testfixture.servlet.MockServletContext;
30+
import org.springframework.web.util.UrlPathHelper;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
3233
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -118,9 +119,13 @@ private void doTestRequestsWithSubPaths(HandlerMapping hm) throws Exception {
118119

119120
@Test
120121
public void requestsWithFullPaths() throws Exception {
122+
123+
UrlPathHelper pathHelper = new UrlPathHelper();
124+
pathHelper.setAlwaysUseFullPath(true);
125+
121126
BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
122127
hm.setPatternParser(null); // the test targets AntPathPatcher-specific feature
123-
hm.setAlwaysUseFullPath(true);
128+
hm.setUrlPathHelper(pathHelper);
124129
hm.setApplicationContext(wac);
125130
Object bean = wac.getBean("godCtrl");
126131

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.web.servlet.HandlerMapping;
3232
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
3333
import org.springframework.web.testfixture.servlet.MockServletContext;
34+
import org.springframework.web.util.UrlPathHelper;
3435
import org.springframework.web.util.WebUtils;
3536

3637
import static org.assertj.core.api.Assertions.assertThat;
@@ -70,8 +71,10 @@ public void handlerBeanNotFound() {
7071
@Test
7172
public void testNewlineInRequest() throws Exception {
7273
Object controller = new Object();
74+
UrlPathHelper urlPathHelper = new UrlPathHelper();
75+
urlPathHelper.setUrlDecode(false);
7376
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(Collections.singletonMap("/*/baz", controller));
74-
mapping.setUrlDecode(false);
77+
mapping.setUrlPathHelper(urlPathHelper);
7578
mapping.setApplicationContext(new StaticApplicationContext());
7679

7780
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-202 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,6 @@
5757
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
5858
import org.springframework.web.util.ServletRequestPathUtils;
5959
import org.springframework.web.util.UrlPathHelper;
60-
import org.springframework.web.util.pattern.PathPatternParser;
6160

6261
import static org.assertj.core.api.Assertions.assertThat;
6362
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -75,10 +74,12 @@ static Stream<?> pathPatternsArguments() {
7574
TestController controller = new TestController();
7675

7776
TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
78-
mapping1.setPatternParser(new PathPatternParser());
77+
78+
UrlPathHelper pathHelper = new UrlPathHelper();
79+
pathHelper.setRemoveSemicolonContent(false);
7980

8081
TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
81-
mapping2.setRemoveSemicolonContent(false);
82+
mapping2.setUrlPathHelper(pathHelper);
8283

8384
return Stream.of(mapping1, mapping2).peek(mapping -> {
8485
mapping.setApplicationContext(new StaticWebApplicationContext());

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,6 @@
5151
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
5252
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
5353
import org.springframework.web.util.ServletRequestPathUtils;
54-
import org.springframework.web.util.pattern.PathPatternParser;
5554

5655
import static org.assertj.core.api.Assertions.assertThat;
5756
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -78,10 +77,10 @@ static Stream<TestRequestMappingInfoHandlerMapping> pathPatternsArguments() {
7877
wac.refresh();
7978

8079
TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
81-
mapping1.setPatternParser(new PathPatternParser());
8280
wac.getAutowireCapableBeanFactory().initializeBean(mapping1, "mapping1");
8381

8482
TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
83+
mapping2.setPatternParser(null);
8584
wac.getAutowireCapableBeanFactory().initializeBean(mapping2, "mapping2");
8685
wac.close();
8786

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public class RequestMappingHandlerMappingTests {
6969
static Stream<Arguments> pathPatternsArguments() {
7070
RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
7171
StaticWebApplicationContext wac1 = new StaticWebApplicationContext();
72-
mapping1.setPatternParser(new PathPatternParser());
7372
mapping1.setApplicationContext(wac1);
7473

75-
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
7674
StaticWebApplicationContext wac2 = new StaticWebApplicationContext();
75+
76+
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
77+
mapping2.setPatternParser(null);
7778
mapping2.setApplicationContext(wac2);
7879

7980
return Stream.of(Arguments.of(mapping1, wac1), Arguments.of(mapping2, wac2));

spring-webmvc/src/test/resources/org/springframework/web/servlet/handler/map3.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
55

66
<bean id="urlMapping1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
7-
<property name="patternParser" >
8-
<bean class="org.springframework.web.util.pattern.PathPatternParser"/>
9-
</property>
107
<property name="mappings"><ref bean="mappings"/></property>
118
</bean>
129

1310
<bean id="urlMapping2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
1411
<property name="patternParser"><null/></property>
15-
<property name="urlDecode"><value>true</value></property>
1612
<property name="mappings"><ref bean="mappings"/></property>
1713
</bean>
1814

0 commit comments

Comments
 (0)