Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to return JSON from ExceptionMapper in RestEasy?

Here's the situation:

  1. I have RESTEasy and Jackson working together beautifully
  2. My RESTful classes implement ExceptionMapper and have a toResponse method on them.
  3. If I have a problem inside any of my RESTful methods an Exception is thrown and caught by the toResponse method.
  4. I'd like the exception to turn into nicely formatted XML or JSON (depending on the Accept header)

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

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

-Dan

like image 270
Dan Avatar asked Apr 15 '11 14:04

Dan


1 Answers

Nevermind, I found the answer. I must have worded my question poorly, but I will leave this around in case someone stumbles upon it.

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

like image 51
Dan Avatar answered Sep 20 '22 01:09

Dan