Spring MVC 异常处理程序只能支持 View 类型的返回类型吗?
@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);
}
上面的代码没有工作。 StackTrace 看起来像这样:
org.springframework.web.util.NestedServletException: 请求处理失败;嵌套的 例外是 java.lang.RuntimeException: errrrrrr 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
@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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须让 Spring 知道如何通过异常处理程序转换返回对象,以便它可以写入 HTTP 响应。
假设“ErrorTO”是一个 JAXB 对象,然后返回的内容类型是 application/xml 您应该在应用程序上下文中创建一个 HandlerExceptionResolver 并配置支持 application/xml 内容类型的消息转换器(例如 org.springframework.http.converter.xml .MarshallingHttpMessageConverter)。这是一个例子:
You have to let Spring know how to convert the return Object by you exception handler so it can write to HTTP response.
Lets say "ErrorTO" is a JAXB object and then returned content type is application/xml you should create a HandlerExceptionResolver in your application context and configure a message converter that support application/xml content type (e.g. org.springframework.http.converter.xml.MarshallingHttpMessageConverter). here is an example:
事实证明这是一个错误,据说在 3.1 中已修复。
turns out to be a bug and said to be fixed in 3.1.