JAX-RS 服务抛出 404 HTTPException 但客户端收到 HTTP 500 代码
JAX-RS service throwing a 404 HTTPException but client receiving a HTTP 500 code
我有一个 RESTful 资源,它调用 EJB 进行查询。如果查询没有结果,EJB 将抛出 EntityNotFoundException。在 catch 块中,它将抛出代码为 404 的 javax.xml.ws.http.HTTPException。
@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 服务时,会打印 EntityNotFoundException
catch 块内的日志消息。但是,我的客户端收到的是 HTTP 代码 500 而不是 404。为什么我没有收到 404 代码?
谢谢,
拉斐尔·阿方索
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
link 中找到它们。它们将属于直接子类之一 ClientErrorException
、RedirectionException
或 ServerErrorException
.
使用 JAX-RS 1.x,此层次结构不存在,因此您需要执行评论中显示的@RafaelAlfonso 之类的操作
throw new WebApplicationException(Response.Status.NOT_FOUND);
还有很多其他可能的构造函数。看看上面的APIlink
我有一个 RESTful 资源,它调用 EJB 进行查询。如果查询没有结果,EJB 将抛出 EntityNotFoundException。在 catch 块中,它将抛出代码为 404 的 javax.xml.ws.http.HTTPException。
@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 服务时,会打印 EntityNotFoundException
catch 块内的日志消息。但是,我的客户端收到的是 HTTP 代码 500 而不是 404。为什么我没有收到 404 代码?
谢谢,
拉斐尔·阿方索
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
link 中找到它们。它们将属于直接子类之一 ClientErrorException
、RedirectionException
或 ServerErrorException
.
使用 JAX-RS 1.x,此层次结构不存在,因此您需要执行评论中显示的@RafaelAlfonso 之类的操作
throw new WebApplicationException(Response.Status.NOT_FOUND);
还有很多其他可能的构造函数。看看上面的APIlink