ExceptionMapper issue in RESTEasy
In my JAXRS application, I have 2 ExceptionMapper
registered as @Provider
public 开发者_如何学JAVAclass MyExceptionMapper implements ExceptionMapper<Exception> {...}
public class MyCustomRuntimeExceptionMapper implements ExceptionMapper<MyCustomRuntimeException> {...}
When my application throws a new MyCustomRuntimeException
, the exception is caught inside the MyExceptionMapper
, even though (JAX-RS spec says) it should be caught inside MyCustomRuntimeExceptionMapper.
Here's what JAXRS says -
JAX-RS supports exception inheritance as well. When an exception is thrown, JAX-RS will first try and find an ExceptionMapper for that exception’s type. If it cannot find one, it will look for a mapper that can handle the exception’s superclass. It will continue this process until there are no more superclasses to match against.
Does anyone have a clue, whats going on here?
I am able to get it right, by declaring providers as below, in my web.xml:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>CustomExceptionHandler,DefaultExceptionHandler</param-value>
</context-param>
I had a problem with this too. It appears as if it finds the superclass of the exception before looking for the subclass. This keeps me from having an ExceptionMapper for Exception. There's an application-specific exception I have to deal with that has subclasses; I can't have separate exception mappers for them. I have one exception mapper for the superclass and then a bunch of "if (e instanceof SubClassA)" statements.
精彩评论