users@jersey.java.net

Re: [Jersey] Returning response in JSON format in case of Exceptions

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 03 May 2010 13:09:53 +0200

Hi,

The code looks fine, but you will need to explicitly convert the
message of the exception to JSON because Jersey will not know how to
do that.

I verified that issue 461 [1] has been fixed in 1.2-SNAPSHOT, which
means, when using that version, you do not have to explicitly set the
content type in the response of the exception mapper if the exception
is thrown from a resource method.

Paul.

[1] https://jersey.dev.java.net/issues/show_bug.cgi?id=461

On Apr 30, 2010, at 4:12 PM, j2eeuser32 wrote:

>
> Hi
>
> I am trying to figure out how to return response in JSON format in
> the case
> of exceptions. I have mapped the exception using ExceptionMapper,
> but the
> overridden toResponse method only returns Response object.
>
> Here is my code. Any help/ideas would be greatly appreciated.
>
> Thanks
>
> <code>
> @Path("/test")
> public class TestResource {
> @GET
> @Path("/userinfo")
> @Produces(MediaType.APPLICATION_JSON)
> public String getUserInfo(@QueryParam("userId") String userId) {
> .....
> throw new TestException("User not available");
> }
> }
>
> @Provider
> public class TestExceptionMapper implements
> ExceptionMapper<TestException> {
> public Response toResponse(TestException ex) {
> return
> Response.status(404).entity(ex.getMessage()).type("application/
> json").build();
> }
> }
> </code>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Returning-response-in-JSON-format-in-case-of-Exceptions-tp4985840p4985840.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>