我有一个 Jersey (2.14) 应用程序,它可以正常工作。我有一些服务在那里运行。现在我想配置 ServletContainer 以便任何未捕获的异常都应该被拦截并记录或通过电子邮件发送到某处。
我已经有一个 ApplicationEventListener 的实现和一个用于生成异常的测试端点。
这是应该生成异常的方法(这是有效的:-):
@GET
@Path(TEST_EXCEPTION)
public String testException(@Context final ServletContext context) {
String s = null;
int size = 0;
if (System.nanoTime() % 10 != 0) {
s = null;
} else {
s = "No exception will occur";
}
size = s.length();
return Integer.toString(size) + ":" + s;
}
这是我的ApplicationEventListener的实现:
public class MyApplicationEventListener implements ApplicationEventListener {
private transient volatile int count = 0;
private int exceptions = 0;
@Override
public void onEvent(final ApplicationEvent applicationEvent) {
ApplicationEvent.Type type = applicationEvent.getType();
}
@Override
public RequestEventListener onRequest(final RequestEvent requestEvent) {
RequestEvent.Type type = requestEvent.getType();
if (type == RequestEvent.Type.ON_EXCEPTION) {
exceptions++;
}
count++;
return null;
}
}
这是我的 web.xml 中的配置:
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com....rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com....filter.MyApplicationEventListener
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.tracing</param-name>
<param-value>ALL</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
onEvent() 和 onRequest() 都被调用了,但是当发生异常时,我没有得到 ON_EXCEPTION,而是 START。
我究竟做错了什么?或者我怎样才能获得由我的 Jersey 服务方法产生的所有异常?
我想拥有/制作类似 Spring 的 HandlerExceptionResolver 的东西。