JAX-RS --- How to return JSON and HTTP Status code together?
- by masato-san
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?