users@jersey.java.net

ExceptionMapper not handling Exception subclasses

From: Gregg Carrier <greggcarrier_at_gmail.com>
Date: Tue, 29 Jun 2010 09:00:11 -0700

Hi all -

Having a problem that I'm sure must be straightforward, but I'm not sure
what I'm doing wrong. I have a set of Exceptions that have a common parent
class. My ExceptionMapper is not called when one of the subclasses is
thrown. Here's what I have:

ExceptionMapper:
@Provider
@Component
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BasicExceptionMapper implements
ExceptionMapper<ParentException> { //ParentException is never thrown

    @Override
    public Response toResponse(ParentException exception) {
        //create a Response ...
    }

}

ParentException:
public class ParentException extends Exception {
    //... some constructors
}

Subclass exception:
public class SpecificException extends ParentException {
    //... some constructors
}

Resource method:
    @POST
    @Path("test")
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response doTest() throws SpecificException {
        if (true) throw new SpecificException();
    }

When I call my resource method, it hangs until the request times out.

If I change the handler signature to handle SpecificException, it works as
expected and returns quickly with the expected response.

So, what do I have wrong? Any help much appreciated. Thanks!

Gregg