【发布时间】:2016-06-07 15:56:03
【问题描述】:
我正在使用带有 Jersey 和 JAX-RS 的 Google App Engine JAVA 开发 REST API。
我希望能够以 JSON 格式向用户发送自定义错误,因为我正在使用 javax.ws.rs.ext.ExceptionMapper
当我在本地机器上使用 Jetty 运行应用程序时一切正常,但是当我部署到 Google 时,我得到了默认的 HTML 404 页面
这里是资源代码:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Customer getCustomer(@PathParam("id") String id) {
Customer customer = getCustomer(id);
if(customer == null)
throw new NotFoundException("Customer not found");
return customer;
}
异常映射器:
@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException e) {
ErrorMessage errorMessage = new ErrorMessage();
errorMessage.setErrrorMessage(e.getMessage());
errorMessage.setResponseCode(404);
return Response.status(Response.Status.NOT_FOUND).entity(errorMessage).type(MediaType.APPLICATION_JSON_TYPE).build();
}
}
我希望得到 JSON 格式的 ErrorMessage 对象作为响应,但我得到的只是默认的 HTML 404 页面。
【问题讨论】:
-
你能指定/粘贴服务器错误堆栈吗?你的代码是否抛出 NotFoundException?
-
Well Jersey 捕获 NotFoundException 并将其映射到 HTTP 404 响应。所以没有栈
-
希望这个链接对你有帮助stackoverflow.com/questions/26903729/…
-
并非如此,我可以修改发生 404 时获得的 HTML 页面。但我希望响应是 JSON 格式的字符串
标签: java google-app-engine exception jersey jax-rs