Browse Source

SPR-4927 - Return 405 instead of 404 when HTTP method is not supported

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1142 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Arjen Poutsma 17 years ago
parent
commit
3854aea64b
  1. 4
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
  2. 48
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java

4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java

@ -446,6 +446,10 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen @@ -446,6 +446,10 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
if (match && mappingInfo.methods.length == 0 && mappingInfo.params.length == 0 &&
resolvedMethodName != null && !resolvedMethodName.equals(handlerMethod.getName())) {
match = false;
} else {
for (RequestMethod requestMethod : mappingInfo.methods) {
allowedMethods.add(requestMethod.toString());
}
}
}
if (match) {

48
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java

@ -183,6 +183,33 @@ public class UriTemplateServletAnnotationControllerTests { @@ -183,6 +183,33 @@ public class UriTemplateServletAnnotationControllerTests {
assertEquals("remove-42", response.getContentAsString());
}
@Test
public void methodNotSupported() throws Exception {
initServlet(MethodNotAllowedController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/1");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
request = new MockHttpServletRequest("POST", "/hotels/1");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(405, response.getStatus());
request = new MockHttpServletRequest("GET", "/hotels");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
request = new MockHttpServletRequest("POST", "/hotels");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(405, response.getStatus());
}
private void initServlet(final Class<?> controllerclass) throws ServletException {
servlet = new DispatcherServlet() {
@Override
@ -337,5 +364,26 @@ public class UriTemplateServletAnnotationControllerTests { @@ -337,5 +364,26 @@ public class UriTemplateServletAnnotationControllerTests {
}
@Controller
@RequestMapping("/hotels")
public static class MethodNotAllowedController {
@RequestMapping(method = RequestMethod.GET)
public void list(Writer writer) {
}
@RequestMapping(method = RequestMethod.GET, value = "{hotelId}")
public void show(@PathVariable long hotelId, Writer writer) {
}
@RequestMapping(method = RequestMethod.PUT, value = "{hotelId}")
public void createOrUpdate(@PathVariable long hotelId, Writer writer) {
}
@RequestMapping(method = RequestMethod.DELETE, value = "/{hotelId}")
public void remove(@PathVariable long hotelId, Writer writer) {
}
}
}

Loading…
Cancel
Save