如何抛出标准的内部服务器异常而不是默认的java.servlet.ServletException?

时间:2019-05-19 04:30:23

标签: java exception jax-rs payload

此问题与Apache OODT项目有关。 有一个JAX-RS Servlet类是通过扩展CXFNonSpringJaxrsServlet创建的。

当与后端服务器的连接丢失时,它将抛出默认的java.servlet.ServletException。我将其更改为以下代码,目的是抛出Custom InternalServerException而不是默认的servlet异常。

@Override
  public void init(ServletConfig configuration) throws WebApplicationException
  {
    try{
      super.init(configuration);
      ServletContext context = configuration.getServletContext();
      initializeClient(context);
      initializeWorkingDir(context);
      initializeConfigurations(context);
    }catch (ServletException e){
      throw new InternalServerErrorException(e.getMessage());
    }
  }

我希望抛出自定义异常。但是在Tomcat9上运行时,它会生成以下异常。它包含servlet和自定义异常部分。根本原因包含自定义异常消息。

Message : Servlet.init() for servlet [CasProductJaxrsServlet] threw exception

Description: The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception :

javax.servlet.ServletException: Servlet.init() for servlet [CasProductJaxrsServlet] threw exception
...

Root Cause :

org.apache.oodt.cas.product.jaxrs.exceptions.InternalServerErrorException: Client could not establish a connection to the file manager.
...

我只想将自定义异常作为JSON有效负载而不是作为堆栈跟踪抛出。

0 个答案:

没有答案
相关问题