users@jersey.java.net

Re: Returning response in JSON format in case of Exceptions

From: j2eeuser32 <nagmidde_at_gmail.com>
Date: Wed, 5 May 2010 13:28:21 -0700 (PDT)

Paul,

Thanks for your reply.

After I posted, I realized I have to set an entity on the Response object
which can be converted into JSON format. Thanks for confirming that.

Here is a variant of the original problem:

When I invoke a REST resource with a different Type of Path Param than what
is expected, it throws a ParamException.

I mapped the ParamException to an ExceptionMapper and set the FaultInfo
Entity(which is annotated using @XMLRootElement) on it.

In my client, irrespective of successful message or failure message, I
always want to retrieve the Response as a String in JSON format like this:

            
    String responseString =
resource.path("/test/dummy").accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
   
But in case of this ParamException, I have to retrieve it like this:

    ClientResponse clientResponse =
resource.path("/test/dummy").accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
    out.println("REST response *** " +
clientResponse.getEntity(String.class));
    
    
What can be done to receive the response as a String?

Appreciate any suggestions/ideas.

Thanks.


Here is the code that I am using:

Exception Mapper
--------------------
@Provider
public class ParamExceptionMapper implements ExceptionMapper<ParamException>
{

    public Response toResponse(ParamException ex) {
        FaultInfo info = new FaultInfo();
        info.setErrorMessage(ex.getMessage());
        info.setErrorCode(400);
        return
Response.status(400).entity(info).type(MediaType.APPLICATION_JSON).build();
    }
}


REST Resource
--------------

@Path("/test")
public class TestResource {
    @GET
    @Path("/{userId}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUserInfo(@PathParam("userId") int userId) {
        .....

        //some code goes here

    }
}

FaultInfo class
---------------

@XmlRootElement(name = "error")
public class FaultInfo implements Serializable {

    private int errorCode;
    private String errorMessage;
    
    //getters and setters for errorCode and errorMessage
    
}


Test JSP, this one throws an UniformInterfaceException, when I pass a String
instead of an int for the userId
<%
        try {
            WebResource resource =
Client.create().resource("http://localhost:8080/myapp");
            String responseString =
resource.path("/test/dummy").accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
            out.println("REST response *** " + responseString);
        } catch (UniformInterfaceException e) {
            out.println("Uniform interface exception ==" + e.getMessage());
        } catch (Throwable t) {
            out.println("Throwable " + t.getMessage());
        }


%>


This test JSP works fine, when I pass a String instead of an int for the
userId, but I have to use ClientResponse.getEntity
to get hold of the Error Message in JSON format.

Gift cart dummy item ==
{"errorCode":0,"errorMessage":"java.lang.NumberFormatException: For input
string: \"dummy\""}

<%
        try {
            WebResource resource =
Client.create().resource("http://localhost:8080/myapp");
            ClientResponse clientResponse =
resource.path("/test/dummy").accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
            out.println("REST response *** " +
clientResponse.getEntity(String.class));
        } catch (UniformInterfaceException e) {
            out.println("Uniform interface exception ==" + e.getMessage());
        } catch (Throwable t) {
            out.println("Throwable " + t.getMessage());
        }


%>
-- 
View this message in context: http://jersey.576304.n2.nabble.com/Returning-response-in-JSON-format-in-case-of-Exceptions-tp4985840p5010958.html
Sent from the Jersey mailing list archive at Nabble.com.