How to return Response with message, jax rs

Viewed 15926

I have a web service rest using jax-rs, my service return a List of objects but y don't know how to add custom status values to response, for example the result that I want to build is the following:

If its ok:

{
   "status": "success",
   "message": "list ok!!",
   "clients": [{
        "name": "john",
        "age": 23
    },
    {
        "name": "john",
        "age": 23
    }]
}

If is error:

{
   "status": "error",
   "message": "not found records",
   "clients": []
}

my rest service:

 @POST
 @Path("/getById")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public List<Client> getById(Client id) {

  try {

       return Response.Ok(new ClientLogic().getById(id)).build();
       //how to add status = success, and message = list! ?

    } catch (Exception ex) {
       return  ??   
       // ex.getMessage() = "not found records"
       //i want return json with satus = error and message from exception
    }
    } 
2 Answers
Related