【发布时间】: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 代码?
谢谢,
拉斐尔·阿方索
【问题讨论】: