Throw Exception in Jersey ResourceMethodInvocationHandlerProvider

438 views Asked by At

Based on this thread Registering a custom ResourceMethodInvocationHandler in Jersey , I was able to intercept Jersey resource method calls after unmarshalling the JSON.

But how do I throw a custom exception based on some logic (Eg: Forbidden exception) in the MyInvocationHandler before invoking the method. When I do that, I am getting 500 error 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;
    }
} 

Getting below error with 500 error code instead of 403 forbidden

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)
0

There are 0 answers