JAX-RS --- How to return JSON and HTTP Status code together?

Posted by masato-san on Stack Overflow See other posts from Stack Overflow or by masato-san
Published on 2011-01-14T01:45:35Z Indexed on 2011/01/14 3:53 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

I'm writing REST web app (Netbean6.9, JAX-RS, Toplink-essential) and trying to return JSON and Http status code.

I have code ready and working just to return JSON when HTTP GET Method is called from client.

Code snippet

 @Path("get/id")
    @GET
    @Produces("application/json")
    public M_?? getMachineToUpdate(@PathParam("id") String id) {

        //some code to return JSON
        .
        .
        return myJson

But I also want to return HTTP status code (500, 200, 204 etc) along with returning JSON.

I tried using HttpServletResponse object,

response.sendError("error message", 500);

But this made browser to think it's real 500 so output web page was regular Http 500 error page.

What I want to is just to return status code so that my Javascript on client side can handle some logic depending on what HTTP status code is returned. (maybe just to display the error code and message on html page.)

Is it possible to do so? or should HTTP status code not be used for such thing?

© Stack Overflow or respective owner

Related posts about java

Related posts about rest