我正在尝试编写一个 JAX-RS 响应过滤器来记录请求和响应详细信息,但 responseContext.getLength(( 总是返回 -1,尽管响应具有正文内容。
这是我的代码:
@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());
}
}
输出:
-1 和"苹果">
记录响应长度的正确方法是什么?
如果你查看 java-docs for the ContextResponseContext
http://docs.oracle.com/javaee/7/api/javax/ws/rs/container/ContainerResponseContext.html#getLength--
内容长度为整数(如果存在且数字有效(。在其他情况下返回 -1。
通过这个,我想很明显为什么它返回 -1。你能在你的响应对象中设置它吗?
另外,如果您只想要回复的长度,您可以执行以下操作: responseContext.getEntity((.length((