以下代码导致在Dropwizard 0.9.2和1.0.2中打印JSON服务器响应:
return ClientBuilder .newBuilder() .build() .register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true))
例如:
Oct 21, 2016 7:57:42 AM org.glassfish.jersey.filter.LoggingFilter log INFO: 1 * Client response received on thread main 1 < 401 1 < Connection: keep-alive 1 < Content-Length: 49 1 < Content-Type: text/plain 1 < Date: Fri, 21 Oct 2016 07:57:42 GMT 1 < Server: […] 1 < WWW-Authenticate: Basic realm="[…]" Credentials are required to access this resource. javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
但是,LoggingFilter在1.0.2中已弃用,建议改为使用LoggingFeature。在LoggingFeature的文档中,它说默认的详细程度是LoggingFeature.Verbosity.PAYLOAD_TEXT,因此我期望以下代码仍能在Dropwizard 1.0.2中打印JSON服务器响应:
LoggingFilter
LoggingFeature
LoggingFeature.Verbosity.PAYLOAD_TEXT
return ClientBuilder .newBuilder() .build() .register(new LoggingFeature(Logger.getLogger(getClass().getName())))
相反,日志仅包含以下内容:
javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
这可以解决问题:
new LoggingFeature(Logger.getLogger(getClass().getName()), Level.OFF, LoggingFeature.Verbosity.PAYLOAD_TEXT, 8192)
我猜想客户端中的日志记录功能就像一个 filter ,而不是预期的include。