Spring MVC 异常处理程序只能支持 View 类型的返回类型吗?

发布于 2024-10-30 20:00:15 字数 1359 浏览 0 评论 0原文

@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)

我查看了 Spring 3 控制器异常处理程序实现问题,基于此,异常处理程序似乎只能返回视图。这是真的吗?

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-11-06 20:00:15

您必须让 Spring 知道如何通过异常处理程序转换返回对象,以便它可以写入 HTTP 响应。
假设“ErrorTO”是一个 JAXB 对象,然后返回的内容类型是 application/xml 您应该在应用程序上下文中创建一个 HandlerExceptionResolver 并配置支持 application/xml 内容类型的消息转换器(例如 org.springframework.http.converter.xml .MarshallingHttpMessageConverter)。这是一个例子:

  <bean id="outboundExceptionAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
    <property name="messageConverters">
      <util:list>
        <ref bean="marshallingHttpMessageConverter"/>
      </util:list>
    </property>
  </bean>

  <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="jaxb2Marshaller" />
    <property name="unmarshaller" ref="jaxb2Marshaller" />
  </bean>

  <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.acme" />
  </bean>

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:

  <bean id="outboundExceptionAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
    <property name="messageConverters">
      <util:list>
        <ref bean="marshallingHttpMessageConverter"/>
      </util:list>
    </property>
  </bean>

  <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="jaxb2Marshaller" />
    <property name="unmarshaller" ref="jaxb2Marshaller" />
  </bean>

  <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.acme" />
  </bean>
山色无中 2024-11-06 20:00:15

事实证明这是一个错误,据说在 3.1 中已修复。

turns out to be a bug and said to be fixed in 3.1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文