简体   繁体   English

如何始终如一地处理球衣和tomcat错误?

[英]How to handle jersey and tomcat errors consistently?

I have implemented some (REST) service with jersey. 我已经使用jersey实现了一些(REST)服务。 If there is a bad request jersey handles the error and answers with some JSON content. 如果存在错误的请求,jersey将处理错误并提供一些JSON内容。 There is an ExceptionMapper that should catch everything: 有一个ExceptionMapper应该捕获所有内容:

public class MyExceptionMapper implements ExceptionMapper<Throwable>

But if there is an invalid HTTP request - eg invalid content type - tomcat handles the exception before jersey has a chance to do so. 但是,如果存在无效的HTTP请求(例如,无效的内容类型),tomcat将在jersey有机会这样做之前处理该异常。 The resulting response is some ugly tomcat HTML error page instead of the desired JSON. 结果响应是一些难看的tomcat HTML错误页面,而不是所需的JSON。

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. 我知道可以在部署描述符中设置<error-page> ,但是我无法访问任何错误详细信息。

Is there a way to prevent tomcat from catching this error? 有没有一种方法可以防止tomcat捕获此错误? If so, jersey could catch it with its ExceptionMapper and return the correct response. 如果是这样,球衣可以使用其ExceptionMapper捕获它并返回正确的响应。

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. 但是,如果将其转发给处理错误的servlet,则应该详细说明错误,并可以将控制权转回jersey等。

ie

web.xml: 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>

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. 注意:在上面的web.xml中,应将java.lang.Throwable替换为遇到的实际异常类型,可以通过“ javax.servlet.error.exception”属性获得该异常类型,如下所示。

InvalidContentHandler.java: 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");
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM