Google App Engine Jersey错误格式为json [英] Google App Engine Jersey error format json

查看:198
本文介绍了Google App Engine Jersey错误格式为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Jersey和JAX-RS开发Google App Engine JAVA的REST API。
我希望能够以JSON格式向用户发送自定义错误,为此我使用 javax.ws.rs.ext.ExceptionMapper



当我在本地机器上使用Jetty运行应用程序时,所有工作都很顺利,但是当我部署到Google时,我得到了默认的HTML 404页面



以下是资源代码:

pre $
@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);
返回客户;
}

异常映射器:

  @Provider 
public class NotFoundExceptionMapper实现ExceptionMapper< NotFoundException> {

@Override
public ResponseResponse(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页面。

//jersey.java.net/apidocs/2.22/jersey/org/glassfish/jersey/server/ServerProperties.html#RESPONSE_SET_STATUS_OVER_SEND_ERRORrel =nofollow> ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR true


每当响应状态为4xx或5xx时可以在 sendError setStatus 之间选择容器特定响应实施。例如。在servlet容器Jersey上可以调用 HttpServletResponse.setStatus(...) HttpServletResponse.sendError(...)


$ b 调用 sendError(...)方法通常会重置实体,响应标题并提供错误页面指定的状态码(例如servlet错误页面配置)。但是,如果您想要后处理响应(例如通过servlet过滤器),唯一的方法是在容器响应对象上调用 setStatus(...)



如果属性值为true,则使用方法 Response.setStatus(...),而不是使用默认的 Response.sendError(...)



属性值的类型是boolean。 默认值为false



配置属性的名称是jersey.config.server.response。 setStatusOverSendError



I'm developing a REST API with Google App Engine JAVA with Jersey and JAX-RS. I want to be able to send custom errors to users in JSON format, for that I'm using 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

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.

解决方案

You can set the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR to true.

Whenever response status is 4xx or 5xx it is possible to choose between sendError or setStatus on container specific Response implementation. E.g. on servlet container Jersey can call HttpServletResponse.setStatus(...) or HttpServletResponse.sendError(...).

Calling sendError(...) method usually resets entity, response headers and provide error page for specified status code (e.g. servlet error-page configuration). However if you want to post-process response (e.g. by servlet filter) the only way to do it is calling setStatus(...) on container Response object.

If property value is true the method Response.setStatus(...) is used over default Response.sendError(...).

Type of the property value is boolean. The default value is false.

The name of the configuration property is "jersey.config.server.response.setStatusOverSendError".

这篇关于Google App Engine Jersey错误格式为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆