Java 防止Tomcat在HTTP错误状态4xx或5xx上干扰Jersey/JAX-RS 2响应体
我有以下RESTAPI堆栈Java 防止Tomcat在HTTP错误状态4xx或5xx上干扰Jersey/JAX-RS 2响应体,java,rest,tomcat7,jax-rs,jersey-2.0,Java,Rest,Tomcat7,Jax Rs,Jersey 2.0,我有以下RESTAPI堆栈 Jersey 2/JAX RS 2.0 Tomcat 7.0.47 杰克逊2 我的目标是在发生错误时有一个自定义的响应主体。我希望能够向客户机发送一份解释,说明到底哪里出了问题,以便于调试 首先,我尝试使用@Context-HttpServletResponse并在那里设置http状态代码,但jersey忽略了它(这是正常行为,但我无法理解) 然后我尝试像这样使用WebApplicationException: @GET @Path("/myapi") publi
- Jersey 2/JAX RS 2.0
- Tomcat 7.0.47
- 杰克逊2
@Context-HttpServletResponse
并在那里设置http状态代码,但jersey忽略了它(这是正常行为,但我无法理解)
然后我尝试像这样使用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
}
<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给了我这样的回答:
@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
}
<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让我一个人呆着,让我自己的回答通过呢?问题是我使用的是ehcache的GzipServlet,它似乎不适合jersey。在多个阶段中,响应包装器抛出异常 这是ehcache的依赖项:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
<version>2.0.4</version>
</dependency>
net.sf.ehcache
org.eclipse.jetty
防波堤小艇
8.1.0.5
web.xml
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
<init-param>
<param-name>mimeTypes</param-name>
<param-value>text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GzipFilter
org.eclipse.jetty.servlets.gzip过滤器
模版
text/html、text/plain、text/xml、application/xhtml+xml、text/css、application/javascript、image/svg+xml
GzipFilter
/*
免责声明:是的,我知道我可以在tomcat配置中激活gzip,但我正在编写一个testserver,它必须能够有带gzip和不带gzip的端点。只需在ResourceConfig类的配置中添加以下代码
property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");
参见Jersey调用HttpServletResponse.sendError()
——并因此调用servlet容器的错误页面
机制似乎违反了JAX-RS 2.0的第3.3.3节,该节规定返回响应
“将导致从响应的实体属性映射的实体体。”.
property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");