基于此线程在 Jersey 中注册自定义 ResourceMethodInvocationHandler,我能够在解组 JSON 后拦截 Jersey 资源方法调用。
但是在调用方法之前,如何在 MyInvocationHandler 中基于某些逻辑(例如:禁止异常)抛出自定义异常。当我这样做时,我收到 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)