The Goal: Don't expose internal stack trace to clientCapture all ErrorWhen we are developing restful API that are exposed to the internet, we better capture all exception/error, and don't expose internal stack trace to outside.In Jersey, we can use ExceptionMapper to capture any kind of exception or error.For known exception such as ProductNotFound, we can capture it, and return meaningful error message to client.But bad things always happen, our application may throw NullPointerExceptionn or exception that we don't ever expect or may throw OutOfMemoryError.When this happens, we need capture it, return some message like: "internal error", and then asynchronously notify engineer owners(send email, store to db or or other approaches).

Code is the King
How Jersey find exception mapper for specific exception?com.sun.jersey.spi.container.ContainerResponse.mapException(Throwable)com.sun.jersey.server.impl.application.ExceptionMapperFactory.find(Class)It checks all exception mappers, get the exception mapper whose exception type is isAssignableFrom and the distance from current exception is smallest.If can't find. it will ResponseListener onError, which by default returns html page with error stack trace.