Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionMapper for NotAllowedException in Liberty

Using JAX-RS 2.0 on Liberty, I'd like to implement some custom exception handling for various javax.ws.rs.WebApplicationException subclasses, such as javax.ws.rs.NotAllowedException for a 405 Method Not Allowed use case.

I've created an ExceptionMapper implementation:

import javax.ws.rs.NotAllowedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class NotAllowedMapper implements ExceptionMapper<NotAllowedException> {

    @Override
    public Response toResponse(NotAllowedException e) {
        return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity("Method Not Allowed")
                .build();
    }

}

And added it to my javax.ws.rs.core.Application subclass:

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/v1.0")
public class JaxRsApplication extends Application {

    /* Other classes and singletons redacted */
    @Override
    public Set<Object> getSingletons() {
        Set<Object> singletons = new HashSet<Object>();

        /* ExceptionMappers for custom exception handling */
        singletons.add(new NotAllowedMapper());

        return singletons;
    }
}

However, when issuing a GET request to an endpoint that only supports POSTs, I get back the default 405 Response, without my custom entity payload.

In the generated Warning message:

[WARNING ] javax.ws.rs.ClientErrorException: HTTP 405 Method Not Allowed...

I see that a javax.ws.rs.ClientErrorException is being thrown, instead of a javax.ws.rs.NotAllowedException.

Creating an ExceptionMapper implementation for ClientErrorException gives me control to specify a custom response like I want, but I'd rather not have to do some kind of if-structure or switch-structure on the status code to handle generic ClientErrorExceptions correctly.

Is there a reason why a ClientErrorException is being thrown, instead of a NotAllowedException ? Or better yet, is there some kind of configuration needed to get NotAllowedException, and other more specific exceptions thrown (when applicable)?

like image 389
austinbruch Avatar asked Oct 22 '25 04:10

austinbruch


1 Answers

The short answer is no, there is no way (currently) to configure Liberty to throw the more-specific NotAllowedException.

Liberty uses Apache CXF as the underlying JAX-RS implementation, and I did a quick scan through the source code, and the only reference to NotAllowedException is in the JAX-RS client code, which wouldn't help you in the server. Looking into the server-side code, it looks like it should not be difficult to modify CXF to throw the NotAllowedException instead of the ClientErrorException. The Liberty organization periodically pulls in updates from the CXF community, so if you open a JIRA request in CXF, we may be able to make the change and pull it in to an upcoming Liberty fix pack.

You can open a CXF JIRA here: https://issues.apache.org/jira/browse/CXF/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel

Hope this helps, Andy

like image 114
Andy McCright Avatar answered Oct 24 '25 18:10

Andy McCright



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!