【发布时间】:2018-02-22 07:14:13
【问题描述】:
我有一个使用 jax-rs 的 Web 服务休息,我的服务返回一个对象列表,但你不知道如何将自定义状态值添加到响应中,例如 我要构建的结果如下:
如果没问题:
{
"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
}
}
【问题讨论】:
标签: java json return jax-rs response