简体   繁体   English

Google App Engine Jersey 错误格式 json

[英]Google App Engine Jersey error format json

I'm developing a REST API with Google App Engine JAVA with Jersey and JAX-RS.我正在使用带有 Jersey 和 JAX-RS 的 Google App Engine JAVA 开发 REST API。 I want to be able to send custom errors to users in JSON format, for that I'm using javax.ws.rs.ext.ExceptionMapper我希望能够以 JSON 格式向用户发送自定义错误,为此我使用javax.ws.rs.ext.ExceptionMapper

All works well when I run the app with Jetty on my local machine, but when I deploy to Google I get the default HTML 404 page当我在本地机器上使用 Jetty 运行应用程序时,一切正常,但是当我部署到 Google 时,我得到了默认的 HTML 404 页面

Here is the resource code:下面是资源代码:

@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;
}

The exception mapper:异常映射器:

@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();
}    
}

I expect to get the JSON formatted ErrorMessage object as response, but all I get is the default HTML 404 page.我希望得到 JSON 格式的 ErrorMessage 对象作为响应,但我得到的只是默认的 HTML 404 页面。

You can set the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR to true .您可以将 Jersey 属性ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR设置为true

Whenever response status is 4xx or 5xx it is possible to choose between sendError or setStatus on container specific Response implementation.当响应状态为 4xx 或 5xx 时,可以在容器特定的Response实现上在sendErrorsetStatus之间进行选择。 Eg on servlet container Jersey can call HttpServletResponse.setStatus(...) or HttpServletResponse.sendError(...) .例如,在 servlet 容器 Jersey 上可以调用HttpServletResponse.setStatus(...)HttpServletResponse.sendError(...)

Calling sendError(...) method usually resets entity, response headers and provide error page for specified status code (eg servlet error-page configuration).调用sendError(...)方法通常会重置实体、响应头并为指定的状态代码(例如 servlet 错误页面配置)提供错误页面 However if you want to post-process response (eg by servlet filter) the only way to do it is calling setStatus(...) on container Response object.但是,如果您想对响应进行后处理(例如通过 servlet 过滤器),唯一的方法是在容器 Response 对象上调用setStatus(...)

If property value is true the method Response.setStatus(...) is used over default Response.sendError(...) .如果属性值为 true,则Response.setStatus(...)方法用于默认Response.sendError(...)

Type of the property value is boolean.属性值的类型是布尔值。 The default value is false .默认值为 false

The name of the configuration property is "jersey.config.server.response.setStatusOverSendError" .配置属性的名称是"jersey.config.server.response.setStatusOverSendError"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM