users@jersey.java.net

Returning response in JSON format in case of Exceptions

From: j2eeuser32 <nagmidde_at_gmail.com>
Date: Fri, 30 Apr 2010 07:12:07 -0700 (PDT)

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.