如何一致地处理球衣和tomcat错误? [英] How to handle jersey and tomcat errors consistently?

查看:156
本文介绍了如何一致地处理球衣和tomcat错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用运动衫实现了一些(REST)服务。如果有一个不好的请求,针织衫处理错误并回答一些JSON内容。有一个ExceptionMapper应该捕获一切:

I have implemented some (REST) service with jersey. If there is a bad request jersey handles the error and answers with some JSON content. There is an ExceptionMapper that should catch everything:

public class MyExceptionMapper implements ExceptionMapper<Throwable>

但是如果HTTP请求无效 - 例如无效内容类型 - tomcat处理异常之前,泽西有机会这样做。所得到的响应是一些丑恶的tomcat HTML错误页面而不是所需的JSON。

But if there is an invalid HTTP request - e.g. invalid content type - tomcat handles the exception before jersey has a chance to do so. The resulting response is some ugly tomcat HTML error page instead of the desired JSON.

我知道可以设置一个< error-页面> 在部署描述符中,但是我无法访问任何错误详细信息。

I know that it is possible to set an <error-page> in the deployment descriptor but there I have no access to any error details.

有没有办法防止tomcat捕获此错误?如果是这样,球衣可以用它的ExceptionMapper抓住它并返回正确的响应。

Is there a way to prevent tomcat from catching this error? If so, jersey could catch it with its ExceptionMapper and return the correct response.

推荐答案

你知道你可以设置一个错误页面但是你指向什么?如果它只是一个静态网页,那么是的,您将无法访问错误详细信息。但是如果将它转发到一个用于处理错误的servlet,那么你应该有关于你的错误的详细信息,并且可以将控制权传回给球衣等。

You know you can set an "error page", but what are you pointing it to? If it's just a static webpage, then yes, you won't have access to error details. But if you forward it to a servlet that is made for handling errors, then you should have details on your your error, and can pass control back to jersey, etc.

web.xml:

  <error-page>
    <error-code>415</error-code>
    <location>/InvalidContentHandler</location>
  </error-page>
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/InvalidContentHandler</location>
  </error-page>

注意:在上述web.xml中,应该替换java。 lang.Throwable与您遇到的实际异常类型,您可以使用javax.servlet.error.exception属性获取,如下所示。

NOTE: in the above web.xml, you should replace java.lang.Throwable with the actual exception type you are encountering, which you can get with the "javax.servlet.error.exception" attribute, shown below.

InvalidContentHandler.java:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processError(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processError(request, response);
}

private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // Pass control to Jersey, or get some info:
    Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
    Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
    String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
    String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
    ...
}

这篇关于如何一致地处理球衣和tomcat错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆