当泽西岛发生 500 服务器错误时,如何获取堆栈跟踪?

use*_*041 2 grizzly maven jersey-2.0

在球衣服务器中时,出现服务器 500 错误:

\n\n
    \n
  • 在服务器响应中,不\xc2\xb4t 有堆栈跟踪或一些信息。

  • \n
  • 在 Eclipse 控制台中,don\xc2\xb4t 有异常堆栈跟踪

  • \n
\n\n

我尝试捕获服务器中的异常并在控制台中打印跟踪,但没有任何反应

\n\n

发生 500 服务器错误时如何获取堆栈跟踪?

\n

Pau*_*tha 6

大多数时候,泛型ExceptionMapper就可以解决问题。

@Provider
public class DebugMapper implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable t) {
        t.printStackTrace();
        return Response.serverError()
            .entity(t.getMessage())
            .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后注册就可以了

ResourceConfig config = new ResourceConfig()
        .register(DebugMapper.class);
Run Code Online (Sandbox Code Playgroud)

有时,当异常未映射时,异常会被 Jersey 吞没,并且您将看不到发生了什么。当问题出现在泽西岛级别时,这通常有效。

也可以看看: