Skip to content

Commit 29c6c9a

Browse files
committed
Add guidance on URI vars and suffix pattern matching
Issue: SPR-11728
1 parent 27153b2 commit 29c6c9a

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/asciidoc/index.adoc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28875,6 +28875,26 @@ configuration. For more information on placeholders, see the javadocs of the
2887528875
`PropertyPlaceholderConfigurer` class.
2887628876

2887728877

28878+
28879+
[[mvc-ann-requestmapping-uri-vars-and-file-extensions]]
28880+
===== URI Variables and Suffix Patterns Matching
28881+
By default Spring MVC automatically performs `".*"` suffix pattern matching so
28882+
that a controller mapped to `/person` is also implicitly mapped to `/person.*`.
28883+
This allows indicating content types via file extensions, e.g. `/person.pdf`,
28884+
`/person.xml`, etc. A common pitfall however is when the last path segment of the
28885+
mapping is a URI variable, e.g. `/person/{id}`. While a request for `/person/1.json`
28886+
would correctly result in path variable id=1 and extension ".json", when the id
28887+
naturally contains a dot, e.g. `/person/[email protected]` the result does not match
28888+
expectations. Clearly here ".com" is not a file extension.
28889+
28890+
The proper way to address this is to configure Spring MVC to only do suffix pattern
28891+
matching against file extensions registered for content negotiation purposes.
28892+
For more on this, first see <<mvc-config-content-negotiation>> and then
28893+
<<mvc-config-path-matching>> showing how to enable suffix pattern matching
28894+
along with how to use registered suffix patterns only.
28895+
28896+
28897+
2887828898
[[mvc-ann-matrix-variables]]
2887928899
===== Matrix Variables
2888028900
The URI specification http://tools.ietf.org/html/rfc3986#section-3.3[RFC 3986] defines
@@ -32572,8 +32592,10 @@ An example of enabling pattern match features and using custom matchers, in Java
3257232592

3257332593
@Override
3257432594
public void configurePathMatch(PathMatchConfigurer configurer) {
32575-
configurer.setUseSuffixPatternMatch(true)
32595+
configurer
32596+
.setUseSuffixPatternMatch(true)
3257632597
.setUseTrailingSlashMatch(false)
32598+
.setUseRegisteredSuffixPatternMatch(true)
3257732599
.setPathMatcher(antPathMatcher())
3257832600
.setUrlPathHelper(urlPathHelper());
3257932601
}
@@ -32603,6 +32625,7 @@ And the same in XML, use the `<mvc:path-matching>` element:
3260332625
<mvc:path-matching
3260432626
suffix-pattern="true"
3260532627
trailing-slash="false"
32628+
registered-suffixes-only="true"
3260632629
path-helper="pathHelper"
3260732630
path-matcher="pathMatcher" />
3260832631
</mvc:annotation-driven>

0 commit comments

Comments
 (0)