SPR-4927 - Return 405 instead of 404 when HTTP method is not supported
This commit is contained in:
parent
752832a8da
commit
0a6cac5a84
|
@ -446,6 +446,10 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||||
if (match && mappingInfo.methods.length == 0 && mappingInfo.params.length == 0 &&
|
if (match && mappingInfo.methods.length == 0 && mappingInfo.params.length == 0 &&
|
||||||
resolvedMethodName != null && !resolvedMethodName.equals(handlerMethod.getName())) {
|
resolvedMethodName != null && !resolvedMethodName.equals(handlerMethod.getName())) {
|
||||||
match = false;
|
match = false;
|
||||||
|
} else {
|
||||||
|
for (RequestMethod requestMethod : mappingInfo.methods) {
|
||||||
|
allowedMethods.add(requestMethod.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (match) {
|
if (match) {
|
||||||
|
|
|
@ -183,6 +183,33 @@ public class UriTemplateServletAnnotationControllerTests {
|
||||||
assertEquals("remove-42", response.getContentAsString());
|
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 {
|
private void initServlet(final Class<?> controllerclass) throws ServletException {
|
||||||
servlet = new DispatcherServlet() {
|
servlet = new DispatcherServlet() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -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…
Reference in New Issue