Jersey 1.8(自定义)记录过滤器

时间:2016-05-26 12:43:18

标签: java spring java-ee logging jersey

web.xml

<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
  <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
  <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>

这不会打印传入的请求实体。它只打印标题和其他东西。请求实体打印为空字符串

然后我从LoggingFilter中获取了一个参考代码,看看它为什么不起作用

    final StringBuilder b = new StringBuilder();
    printRequestLine(b, request);
    printRequestHeaders(b, request.getRequestHeaders());


    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = request.getEntityInputStream();
    try {
        if(in.available() > 0) {
            ReaderWriter.writeTo(in, out);

            byte[] requestEntity = out.toByteArray();
            printEntity(b, requestEntity);

            request.setEntityInputStream(new ByteArrayInputStream(requestEntity));
        }
        return request;
    } catch (IOException ex) {
        throw new ContainerException(ex);
    } finally {
        logger.info(b.toString());
    }

InputStream持有

的对象
org.apache.catalina.connector.CoyoteInputStream

如果删除条件if(in.available()&gt; 0)此代码有效并打印请求

in.available()始终返回0,其中输入流保持传入请求

如果删除该条件,代码可能会失败? 那是什么原因它没有给我可用()值?

1 个答案:

答案 0 :(得分:0)

默认情况下,日志记录筛选器不会打印实体。有一个构造函数可以启用它。

https://jersey.java.net/apidocs/2.11/jersey/org/glassfish/jersey/filter/LoggingFilter.html#LoggingFilter(java.util.logging.Logger,布尔值)