Search code examples
javajakarta-eejettyjax-rsjava-ee-7

How to add headers to HTTP response within ClientErrorException?


I have a custom ClientErrorException and I want to add some specific headers to the response when this exception is thrown.

For example, I can specify status of the response in the constructor:

public ClientErrorException(String message, Response.Status status) {
...
}

But how to specify headers? What the proper way to do that?


Solution

  • You can use an ExceptionMapper for jax-rs to return a custom response when an exception is thrown.

    There is a nice example in here