diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java index ea05dbf6497..ffb61e7901d 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java @@ -637,8 +637,11 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator Map variables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); + + int patternVariableCount = StringUtils.countOccurrencesOf(mappedPattern, "{"); - if (CollectionUtils.isEmpty(variables) && pathMatcher.match(mappedPattern, lookupPath)) { + if ( (variables == null || patternVariableCount != variables.size()) + && pathMatcher.match(mappedPattern, lookupPath)) { variables = pathMatcher.extractUriTemplateVariables(mappedPattern, lookupPath); request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, variables); } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java index c3fe460aadb..2b436092e32 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java @@ -376,6 +376,36 @@ public class UriTemplateServletAnnotationControllerTests { assertEquals("plain-bar", response.getContentAsString()); } + /* + * See SPR-6978 + */ + @Test + public void doIt() throws Exception { + initServlet(Spr6978Controller.class); + + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo/100"); + MockHttpServletResponse response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals("loadEntity:foo:100", response.getContentAsString()); + + request = new MockHttpServletRequest("POST", "/foo/100"); + response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals("publish:foo:100", response.getContentAsString()); + + request = new MockHttpServletRequest("GET", "/module/100"); + response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals("loadModule:100", response.getContentAsString()); + + request = new MockHttpServletRequest("POST", "/module/100"); + response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals("publish:module:100", response.getContentAsString()); + + } + + /* * Controllers @@ -625,5 +655,26 @@ public class UriTemplateServletAnnotationControllerTests { } } + @Controller + public static class Spr6978Controller { + + @RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET) + public void loadEntity(@PathVariable final String type, @PathVariable final long id, Writer writer) + throws IOException { + writer.write("loadEntity:" + type + ":" + id); + } + + @RequestMapping(value = "/module/{id}", method = RequestMethod.GET) + public void loadModule(@PathVariable final long id, Writer writer) throws IOException { + writer.write("loadModule:" + id); + } + + @RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST) + public void publish(@PathVariable final String type, @PathVariable final long id, Writer writer) + throws IOException { + writer.write("publish:" + type + ":" + id); + } + } + }