I want to wrap underlaying RuntimeExceptions
to a custom json format , making the servlet container won't dump the stacktrace to client.
I follow this question : JAX-RS (Jersey) custom exception with XML or JSON . When calling :
try {
doSomething(parameters);
}
catch(RuntimeException e) {
throw new MyCustomException(500 , e.getMessage() , Status.INTERNAL_SERVER_ERROR);
}
When I intentionally feed wrong parameters (and trigger RuntimeException
thrown by doSomething()
) , I didn't see MyCustomExceptionMapper
working. Instead , the servlet container dumps :
The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
api.MyCustomException: (underlaying msgs)
The MyCustomExceptionMapper
is indeed registered in the javax.ws.rs.core.Application
:
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(other classes);
set.add(MyCustomExceptionMapper.class);
return set;
}
What did I miss ?
Thanks a lot !
Environment : JAX-RS , jersey-server 1.5
classes spec :
class MyCustomException extends RuntimeException
@Provider
class MyCustomExceptionMapper implements ExceptionMapper<MyCustomException>
updated :
I suspect that Application.getClasses()
is never called , so I add some println messages :
@Override
public Set<Class<?>> getClasses()
{
System.out.println("\n\n\n\n ApiConfig getClasses");
}
And in deed , it's never shown !
I am sure this ApiConfig is in the web.xml :
<context-param>
<param-name>javax.ws.rs.core.Application</param-name>
<param-value>destiny.web.api.ApiConfig</param-value>
</context-param>
But why it seems Jersey never calls it ?