我有一個使用jax-rs的web服務休息,我的服務返回一個對象列表,但是不知道如何將自定義狀態值添加到響應中,例如 結果我想打造的是以下幾點:如何使用消息返回響應,jax rs
如果其確定:
{
"status": "success",
"message": "list ok!!",
"clients": [{
"name": "john",
"age": 23
},
{
"name": "john",
"age": 23
}]
}
如果是錯誤:
{
"status": "error",
"message": "not found records",
"clients": []
}
我休息服務:
@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
}
}