【发布时间】:2014-02-03 18:10:16
【问题描述】:
我有以下 REST API 堆栈
- 泽西 2/JAX RS 2.0
- Tomcat 7.0.47
- 杰克逊 2
我的目标是在发生错误时有一个自定义响应正文。我希望能够向客户发送一个解释究竟出了什么问题,以便于调试。
首先我尝试使用@Context HttpServletResponse并在那里设置http状态码,但它被球衣忽略了(这是正常行为,但这超出了我的理解)
然后我尝试像这样使用WebApplicationException:
@GET
@Path("/myapi")
public BaseResponse getSomething() {
BaseResponse b = new BaseResponse();
if(error) {
b.setStatusDescription("reason for error");
throw new WebApplicationException(Response.status(Response.Status.CONFLICT).entity(b).build());
}
//add content to BaseReponse
return b
}
但是 Tomcat 给我的回报是这样的:
<html>
<head>
<title>Apache Tomcat/7.0.47 - Error report</title>
<style>
<!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22p
这是标准 Tomcat html 输出,由我想要返回的响应正文的 contet-length(.entity(b) - b 的长度)限制。所以它被识别了,但 Tomcat 只是用自己的错误页面覆盖它。
作为旁注,我还尝试返回具有相同结果的响应:
return Response.status(Response.Status.CONFLICT).entity(b).build()
那么我如何告诉 Tomcat 让我一个人呆着,让我自己做出回应?
【问题讨论】:
-
那个 Jersey 调用
HttpServletResponse.sendError()——并因此调用 servlet 容器的error-page机制——似乎违反了 JAX-RS 2.0 的第 3.3.3 节,该节规定返回Response“导致从Response"的实体属性映射的实体主体。
标签: java rest tomcat7 jax-rs jersey-2.0