@RequestMapping(value = "/testerror", method = RequestMethod.GET)
public
@ResponseBody
ErrorTO testerror(HttpServletRequest request, HttpServletResponse response) {
throw new RuntimeException("erorrrrrr");
}
@ExceptionHandler(RuntimeException.class)
public @ResponseBody ErrorTO handlePoprocksExceptionAsReponseBody(RuntimeException ex,
HttpServletRequest request, HttpServletResponse response) {
response.setStatus(response.SC_BAD_REQUEST);
return new ErrorTO(ex.getMessage(), -999);
}
The above code did not work. StackTrace looked like this:
org.springframework.web.util.NestedServletException:
Request processing failed; nested
exception is
java.lang.RuntimeException: erorrrrrr
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
I looked at Spring 3 controller exception handler implementation problems , and based on that it seems to be that exception handlers can only return views. Is that true?
turns out to be a bug and said to be fixed in 3.1.