【发布时间】:2015-09-06 01:48:54
【问题描述】:
我正在开发 Rest 服务,如果出现故障/错误,我应该使用 Response 还是 WebApplicationException?
下面是一个示例,我使用 Response 返回 400 Bad Request 错误消息以及错误的 JSON 响应。
@GET
@Path("/data")
@Produces(MediaType.APPLICATION_JSON)
public Response getData(@Context UriInfo uriInfo) {
String client = getClientId();
if (client == null || client.isEmpty()) {
return Response.status(HttpServletResponse.SC_BAD_REQUEST)
.entity("{\"error\": \"client id is null or empty\"}").build();
}
// some code and return succesfull response
return Response
.status(HttpServletResponse.SC_OK)
.entity(resp.getResponse()).build();
}
在这里做什么是正确的?如果我需要使用WebApplicationException,那么如何使用它来返回 400 Bad Request 错误消息以及 JSON 响应
【问题讨论】:
标签: java json rest jersey jax-rs