基于这个线程Registering a custom ResourceMethodInvocationHandler in Jersey,我能够在解组JSON之后拦截Jersey资源方法调用。
但是如何在调用方法之前在MyInvocationHandler中基于某些逻辑(例如:Forbidden exception)抛出自定义异常。当我这样做时,我收到500错误javax.servlet.ServletException
:
public class MyIncovationHandler implements InvocationHandler {
@Override
public Object invoke(Object obj, Method method, Object[] args)
throws Throwable {
if(!verifyAccess(args)) {
throw new ForbiddenException();
}
Object result = method.invoke(obj, args);
return result;
}
}
使用500错误代码而不是403禁止
获得以下错误javax.servlet.ServletException: javax.ws.rs.ProcessingException: javax.ws.rs.ProcessingException: javax.ws.rs.ProcessingException: javax.ws.rs.ForbiddenException: HTTP 403 Forbidden
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:421)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)