1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import org .springframework .lang .Nullable ;
24
24
import org .springframework .util .AntPathMatcher ;
25
25
import org .springframework .util .PathMatcher ;
26
- import org .springframework .web .servlet .handler .AbstractHandlerMapping ;
27
26
import org .springframework .web .servlet .mvc .method .annotation .RequestMappingHandlerMapping ;
28
27
import org .springframework .web .util .UrlPathHelper ;
29
28
import org .springframework .web .util .pattern .PathPattern ;
34
33
* <ul>
35
34
* <li>{@link WebMvcConfigurationSupport#requestMappingHandlerMapping}</li>
36
35
* <li>{@link WebMvcConfigurationSupport#viewControllerHandlerMapping}</li>
36
+ * <li>{@link WebMvcConfigurationSupport#beanNameHandlerMapping}</li>
37
+ * <li>{@link WebMvcConfigurationSupport#routerFunctionMapping}</li>
37
38
* <li>{@link WebMvcConfigurationSupport#resourceHandlerMapping}</li>
38
39
* </ul>
39
40
*
40
41
* @author Brian Clozel
42
+ * @author Rossen Stoyanchev
41
43
* @since 4.0.3
42
44
*/
43
45
public class PathMatchConfigurer {
44
46
47
+ private boolean preferPathMatcher = false ;
48
+
45
49
@ Nullable
46
50
private PathPatternParser patternParser ;
47
51
@@ -74,17 +78,28 @@ public class PathMatchConfigurer {
74
78
75
79
76
80
/**
77
- * Enable use of parsed {@link PathPattern}s as described in
78
- * {@link AbstractHandlerMapping#setPatternParser(PathPatternParser)}.
79
- * <p><strong>Note:</strong> This is mutually exclusive with use of
80
- * {@link #setUrlPathHelper(UrlPathHelper)} and
81
- * {@link #setPathMatcher(PathMatcher)}.
82
- * <p>By default this is not enabled.
81
+ * Set the {@link PathPatternParser} to parse {@link PathPattern patterns}
82
+ * with for URL path matching. Parsed patterns provide a more modern and
83
+ * efficient alternative to String path matching via {@link AntPathMatcher}.
84
+ * <p><strong>Note:</strong> This property is mutually exclusive with the
85
+ * following other, {@code AntPathMatcher} related properties:
86
+ * <ul>
87
+ * <li>{@link #setUseSuffixPatternMatch(Boolean)}
88
+ * <li>{@link #setUseRegisteredSuffixPatternMatch(Boolean)}
89
+ * <li>{@link #setUrlPathHelper(UrlPathHelper)}
90
+ * <li>{@link #setPathMatcher(PathMatcher)}
91
+ * </ul>
92
+ * <p>By default, as of 6.0, a {@link PathPatternParser} with default
93
+ * settings is used, which enables parsed {@link PathPattern patterns}.
94
+ * Set this property to {@code null} to fall back on String path matching via
95
+ * {@link AntPathMatcher} instead, or alternatively, setting one of the above
96
+ * listed {@code AntPathMatcher} related properties has the same effect.
83
97
* @param patternParser the parser to pre-parse patterns with
84
98
* @since 5.3
85
99
*/
86
- public PathMatchConfigurer setPatternParser (PathPatternParser patternParser ) {
100
+ public PathMatchConfigurer setPatternParser (@ Nullable PathPatternParser patternParser ) {
87
101
this .patternParser = patternParser ;
102
+ this .preferPathMatcher = (patternParser == null );
88
103
return this ;
89
104
}
90
105
@@ -120,9 +135,11 @@ public PathMatchConfigurer addPathPrefix(String prefix, Predicate<Class<?>> pred
120
135
/**
121
136
* Whether to use suffix pattern match (".*") when matching patterns to
122
137
* requests. If enabled a method mapped to "/users" also matches to "/users.*".
138
+ * <p><strong>Note:</strong> This property is mutually exclusive with
139
+ * {@link #setPatternParser(PathPatternParser)}. If set, it enables use of
140
+ * String path matching, unless a {@code PathPatternParser} is also
141
+ * explicitly set in which case this property is ignored.
123
142
* <p>By default this is set to {@code false}.
124
- * <p><strong>Note:</strong> This property is mutually exclusive with and
125
- * ignored when {@link #setPatternParser(PathPatternParser)} is set.
126
143
* @deprecated as of 5.2.4. See class-level note in
127
144
* {@link RequestMappingHandlerMapping} on the deprecation of path extension
128
145
* config options. As there is no replacement for this method, in 5.2.x it is
@@ -132,6 +149,7 @@ public PathMatchConfigurer addPathPrefix(String prefix, Predicate<Class<?>> pred
132
149
@ Deprecated
133
150
public PathMatchConfigurer setUseSuffixPatternMatch (Boolean suffixPatternMatch ) {
134
151
this .suffixPatternMatch = suffixPatternMatch ;
152
+ this .preferPathMatcher |= suffixPatternMatch ;
135
153
return this ;
136
154
}
137
155
@@ -141,41 +159,66 @@ public PathMatchConfigurer setUseSuffixPatternMatch(Boolean suffixPatternMatch)
141
159
* {@link WebMvcConfigurer#configureContentNegotiation configure content
142
160
* negotiation}. This is generally recommended to reduce ambiguity and to
143
161
* avoid issues such as when a "." appears in the path for other reasons.
162
+ * <p><strong>Note:</strong> This property is mutually exclusive with
163
+ * {@link #setPatternParser(PathPatternParser)}. If set, it enables use of
164
+ * String path matching, unless a {@code PathPatternParser} is also
165
+ * explicitly set in which case this property is ignored.
144
166
* <p>By default this is set to "false".
145
- * <p><strong>Note:</strong> This property is mutually exclusive with and
146
- * ignored when {@link #setPatternParser(PathPatternParser)} is set.
147
167
* @deprecated as of 5.2.4. See class-level note in
148
168
* {@link RequestMappingHandlerMapping} on the deprecation of path extension
149
169
* config options.
150
170
*/
151
171
@ Deprecated
152
172
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch (Boolean registeredSuffixPatternMatch ) {
153
173
this .registeredSuffixPatternMatch = registeredSuffixPatternMatch ;
174
+ this .preferPathMatcher |= registeredSuffixPatternMatch ;
154
175
return this ;
155
176
}
156
177
157
178
/**
158
179
* Set the UrlPathHelper to use to resolve the mapping path for the application.
159
- * <p><strong>Note:</strong> This property is mutually exclusive with and
160
- * ignored when {@link #setPatternParser(PathPatternParser)} is set.
180
+ * <p><strong>Note:</strong> This property is mutually exclusive with
181
+ * {@link #setPatternParser(PathPatternParser)}. If set, it enables use of
182
+ * String path matching, unless a {@code PathPatternParser} is also
183
+ * explicitly set in which case this property is ignored.
184
+ * <p>By default this is an instance of {@link UrlPathHelper} with default
185
+ * settings.
161
186
*/
162
187
public PathMatchConfigurer setUrlPathHelper (UrlPathHelper urlPathHelper ) {
163
188
this .urlPathHelper = urlPathHelper ;
189
+ this .preferPathMatcher = true ;
164
190
return this ;
165
191
}
166
192
167
193
/**
168
194
* Set the PathMatcher to use for String pattern matching.
169
- * <p>By default this is {@link AntPathMatcher}.
170
- * <p><strong>Note:</strong> This property is mutually exclusive with and
171
- * ignored when {@link #setPatternParser(PathPatternParser)} is set.
195
+ * <p><strong>Note:</strong> This property is mutually exclusive with
196
+ * {@link #setPatternParser(PathPatternParser)}. If set, it enables use of
197
+ * String path matching, unless a {@code PathPatternParser} is also
198
+ * explicitly set in which case this property is ignored.
199
+ * <p>By default this is an instance of {@link AntPathMatcher} with default
200
+ * settings.
172
201
*/
173
202
public PathMatchConfigurer setPathMatcher (PathMatcher pathMatcher ) {
174
203
this .pathMatcher = pathMatcher ;
204
+ this .preferPathMatcher = true ;
175
205
return this ;
176
206
}
177
207
178
208
209
+ /**
210
+ * Whether to prefer {@link PathMatcher}. This is the case when either is true:
211
+ * <ul>
212
+ * <li>{@link PathPatternParser} is explicitly set to {@code null}.
213
+ * <li>{@link PathPatternParser} is not explicitly set, and a
214
+ * {@link PathMatcher} related option is explicitly set.
215
+ * </ul>
216
+ * @since 6.0
217
+ */
218
+ protected boolean preferPathMatcher () {
219
+ return (this .patternParser == null && this .preferPathMatcher );
220
+ }
221
+
179
222
/**
180
223
* Return the {@link PathPatternParser} to use, if configured.
181
224
* @since 5.3
0 commit comments