Respect ModelAndView.status in @ExceptionHandler

Issue: SPR-14006
This commit is contained in:
Rossen Stoyanchev
2016-10-24 15:59:54 +01:00
parent 664ef756c2
commit c062835702
3 changed files with 34 additions and 2 deletions
@@ -1752,6 +1752,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);
@@ -3320,6 +3332,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 {
}
}