SPR-6978 - Dispatcher fails to invoke handler method when request method conflicts with request path

This commit is contained in:
Arjen Poutsma 2010-03-22 14:12:32 +00:00
parent b07d02f1bf
commit 4c0744ee54
2 changed files with 55 additions and 1 deletions

View File

@ -638,7 +638,10 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
Map<String, String> variables =
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (CollectionUtils.isEmpty(variables) && pathMatcher.match(mappedPattern, lookupPath)) {
int patternVariableCount = StringUtils.countOccurrencesOf(mappedPattern, "{");
if ( (variables == null || patternVariableCount != variables.size())
&& pathMatcher.match(mappedPattern, lookupPath)) {
variables = pathMatcher.extractUriTemplateVariables(mappedPattern, lookupPath);
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, variables);
}

View File

@ -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);
}
}
}