Easy way to return JSON from ExceptionMapper to RestEasy?

Here's the situation:

  • My RESTEasy and Jackson work great together.
  • My RESTful classes implement ExceptionMapper and have a toResponse method for them.
  • If I have a problem inside any of my RESTful methods, an Exception is thrown and caught by the toResponse method.
  • I want the exception to turn into well-formatted XML or JSON (depending on the Accept header)

The problem is that I have no idea that the Accept header is inside the toResponse method. I know that I can extend the Exception class and pass the header as a variable that I can use in toResponse, but I was hoping there would be a more β€œcorrect” way to do this. I tried adding the @Produces annotation to the toResponse method without success.

Has anyone successfully configured RESTEasy to easily return JSON or XML from a response?

Dan

+6
source share
1 answer

Nothing, I found the answer. I must have incorrectly formulated my question, but I will leave it in case someone stumbles upon it.

Here is the answer: JAX-RS (Jersey) custom exception with XML or JSON

+10
source

Source: https://habr.com/ru/post/885993/


All Articles