SPR-5636 - @RequestMapping matching should be insensitive to trailing slashes
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1018 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
138e17d620
commit
6f016dd18a
|
|
@ -503,7 +503,15 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||||
if (pattern.equals(lookupPath) || pathMatcher.match(pattern, lookupPath)) {
|
if (pattern.equals(lookupPath) || pathMatcher.match(pattern, lookupPath)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return !(pattern.indexOf('.') != -1) && pathMatcher.match(pattern + ".*", lookupPath);
|
boolean hasSuffix = pattern.indexOf('.') != -1;
|
||||||
|
if (!hasSuffix && pathMatcher.match(pattern + ".*", lookupPath)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
boolean endsWithSlash = pattern.endsWith("/");
|
||||||
|
if (!endsWithSlash && pathMatcher.match(pattern + "/", lookupPath)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkParameters(RequestMappingInfo mapping, HttpServletRequest request) {
|
private boolean checkParameters(RequestMappingInfo mapping, HttpServletRequest request) {
|
||||||
|
|
|
||||||
|
|
@ -86,10 +86,10 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether to register paths using the default suffix pattern as well:
|
* Set whether to register paths using the default suffix pattern as well:
|
||||||
* i.e. whether "/users" should be registered as "/users.*" too.
|
* i.e. whether "/users" should be registered as "/users.*" and "/users/" too.
|
||||||
* <p>Default is "true". Turn this convention off if you intend to interpret
|
* <p>Default is "true". Turn this convention off if you intend to interpret
|
||||||
* your <code>@RequestMapping</code> paths strictly.
|
* your <code>@RequestMapping</code> paths strictly.
|
||||||
* <p>Note that paths which include a ".xxx" suffix already will not be
|
* <p>Note that paths which include a ".xxx" suffix or end with "/" already will not be
|
||||||
* transformed using the default suffix pattern in any case.
|
* transformed using the default suffix pattern in any case.
|
||||||
*/
|
*/
|
||||||
public void setUseDefaultSuffixPattern(boolean useDefaultSuffixPattern) {
|
public void setUseDefaultSuffixPattern(boolean useDefaultSuffixPattern) {
|
||||||
|
|
@ -168,8 +168,9 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
|
||||||
*/
|
*/
|
||||||
protected void addUrlsForPath(Set<String> urls, String path) {
|
protected void addUrlsForPath(Set<String> urls, String path) {
|
||||||
urls.add(path);
|
urls.add(path);
|
||||||
if (this.useDefaultSuffixPattern && path.indexOf('.') == -1) {
|
if (this.useDefaultSuffixPattern && path.indexOf('.') == -1 && !path.endsWith("/")) {
|
||||||
urls.add(path + ".*");
|
urls.add(path + ".*");
|
||||||
|
urls.add(path + "/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,11 @@ public class UriTemplateServletAnnotationControllerTests {
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
assertEquals("list", response.getContentAsString());
|
assertEquals("list", response.getContentAsString());
|
||||||
|
|
||||||
|
request = new MockHttpServletRequest("GET", "/hotels/");
|
||||||
|
response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals("list", response.getContentAsString());
|
||||||
|
|
||||||
request = new MockHttpServletRequest("POST", "/hotels");
|
request = new MockHttpServletRequest("POST", "/hotels");
|
||||||
response = new MockHttpServletResponse();
|
response = new MockHttpServletResponse();
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
|
|
@ -162,6 +167,11 @@ public class UriTemplateServletAnnotationControllerTests {
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
assertEquals("show-42", response.getContentAsString());
|
assertEquals("show-42", response.getContentAsString());
|
||||||
|
|
||||||
|
request = new MockHttpServletRequest("GET", "/hotels/42/");
|
||||||
|
response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals("show-42", response.getContentAsString());
|
||||||
|
|
||||||
request = new MockHttpServletRequest("PUT", "/hotels/42");
|
request = new MockHttpServletRequest("PUT", "/hotels/42");
|
||||||
response = new MockHttpServletResponse();
|
response = new MockHttpServletResponse();
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue