[英]JAX-RS response filter > getLength() is always -1
I am trying to write a JAX-RS response filter to log the request and response details but responseContext.getLength() always returns with -1 despite of response has body content. 我正在尝试编写一个JAX-RS响应过滤器来记录请求和响应的详细信息,尽管响应具有主体内容,但responseContext.getLength()始终返回-1。
Here is my code: 这是我的代码:
@Override
public void filter(final ContainerRequestContext requestContext,
final ContainerResponseContext responseContext) throws IOException {
System.out.println(responseContext.getLength());
if (responseContext.hasEntity()) {
System.out.println(responseContext.getEntity());
}
}
Output: 输出:
-1 and "apple" -1和“苹果”
What is the correct way to log the length of the response? 记录响应长度的正确方法是什么?
If you look at the java-docs for the ContextResponseContext 如果您查看ContextResponseContext的java-docs
http://docs.oracle.com/javaee/7/api/javax/ws/rs/container/ContainerResponseContext.html#getLength-- http://docs.oracle.com/javaee/7/api/javax/ws/rs/container/ContainerResponseContext.html#getLength--
Content-Length as integer if present and valid number. Content-Length为整数(如果存在)和有效数字。 In other cases returns -1. 在其他情况下,返回-1。
By this, I guess it's pretty obvious why it returns -1. 这样,我想它为什么返回-1很明显。 Can you set it in your response object? 您可以在响应对象中进行设置吗?
Also, if you just want the length of your response, may be you can do: responseContext.getEntity().length() 另外,如果您只想要响应的长度,则可以执行以下操作:responseContext.getEntity()。length()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.