【问题标题】:JAX-RS service throwing a 404 HTTPException but client receiving a HTTP 500 codeJAX-RS 服务抛出 404 HTTPException 但客户端收到 HTTP 500 代码
【发布时间】:2015-04-23 20:20:17
【问题描述】:

我有一个 RESTful 资源,它调用 EJB 进行查询。如果查询没有结果,EJB 将抛出 EntityNotFoundException。在 catch 块中,它会抛出一个 javax.xml.ws.http.HTTPException,代码为 404。

@Stateless
@Path("naturezas")
public class NaturezasResource {

    @GET
    @Path("list/{code}")
    @Produces(MediaType.APPLICATION_JSON)
    public String listByLista(
            @PathParam("code") codeListaNaturezasEnum code) {
        try {
            List<NaturezaORM> naturezas = this.naturezaSB
                    .listByListaNaturezas(code);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(naturezas);
        } catch (EntityNotFoundException e) { // No data found
            logger.error("there is no Natures with the code " + code);
            throw new HTTPException(404);
        } catch (Exception e) { // Other exceptions
            e.printStackTrace();
            throw new HTTPException(500);
        }
    }

}

当我使用没有结果的代码调用 Rest Service 时,会打印 EntityNotFoundException catch 块内的日志消息。但是,我的客户端收到的是 HTTP 代码 500 而不是 404。为什么我没有收到 404 代码?

谢谢,

拉斐尔·阿方索

【问题讨论】:

    标签: java jax-rs


    【解决方案1】:

    javax.xml.ws.http.HTTPException 用于 JAX-WS。默认情况下,JAX-RS 不知道如何处理它,除非您为其编写 ExceptionMapper。所以异常会冒泡到容器级别,它只是发送一个通用的内部服务器错误响应。

    改为使用WebApplicationException 或其子类之一。这里列出了层次结构中包含的异常,以及它们映射到的内容(注意:这仅在 JAX-RS 2 中)

    Exception                      Status code    Description
    -------------------------------------------------------------------------------
    BadRequestException            400            Malformed message
    NotAuthorizedException         401            Authentication failure
    ForbiddenException             403            Not permitted to access
    NotFoundException              404            Couldn’t find resource
    NotAllowedException            405            HTTP method not supported
    NotAcceptableException         406            Client media type requested 
                                                                not supported
    NotSupportedException          415            Client posted media type 
                                                                not supported
    InternalServerErrorException   500            General server error
    ServiceUnavailableException    503            Server is temporarily unavailable 
                                                                or busy
    

    您也可以在上面的WebApplicationException 链接中找到它们。它们将属于ClientErrorExceptionRedirectionExceptionServerErrorException 的直接子类之一。

    在 JAX-RS 1.x 中,这种层次结构不存在,因此您需要执行类似 @RafaelAlfonso 在评论中显示的操作

    throw new WebApplicationException(Response.Status.NOT_FOUND);
    

    还有很多其他可能的构造函数。看看上面的 API 链接就可以了

    【讨论】:

    • 抛出新的 WebApplicationException(Response.status(404).build());
    • 对于 JAX-RS 2,我倾向于使用 NotFoundException,它是一个子类,并被映射到 404 :-)
    • 我做了throw new WebApplicationException(e, Response.Status.NOT_FOUND);
    猜你喜欢
    • 2013-06-03
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多