Respect ModelAndView.status in @ExceptionHandler

Issue: SPR-14006
This commit is contained in:
Rossen Stoyanchev
2016-10-24 15:59:54 +01:00
parent aea3a75018
commit efe3996cf9
3 changed files with 34 additions and 2 deletions

View File

@@ -1742,6 +1742,18 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
assertEquals("view", response.getForwardedUrl());
}
@Test // SPR-14796
public void modelAndViewWithStatusInExceptionHandler() throws Exception {
initServletWithControllers(ModelAndViewController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/exception");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(422, response.getStatus());
assertEquals("view", response.getForwardedUrl());
}
@Test
public void httpHead() throws ServletException, IOException {
initServletWithControllers(ResponseEntityController.class);
@@ -3302,6 +3314,20 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
public ModelAndView methodWithHttpStatus(MyEntity object) {
return new ModelAndView("view", new ModelMap(), HttpStatus.UNPROCESSABLE_ENTITY);
}
@RequestMapping("/exception")
public void raiseException() throws Exception {
throw new TestException();
}
@ExceptionHandler(TestException.class)
public ModelAndView handleException() {
return new ModelAndView("view", new ModelMap(), HttpStatus.UNPROCESSABLE_ENTITY);
}
@SuppressWarnings("serial")
private static class TestException extends Exception {
}
}
}