[英]Log the request body of JSON @ Jersey Rest WS
如何使用Java的Jersey代码从以下代码@ Rest WS打印/记录JSON的请求正文。 我可以为此记录日志。 我正在努力记录/打印确切的请求正文。
提前致谢。
WebTarget target = client.target(url);
//authentication strings
String authString = "username:password";
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
Response res = target.request(MediaType.APPLICATION_JSON)
.header("content-type", "application/json")
.header("Authorization", "Basic " + authStringEnc)
.header("accept", "application/json,text/plain")
.header("X-senderTimestamp", new Timestamp(System.currentTimeMillis()))
.header("X-appCode", "IVR")
.header("X-sessionId", XsessionId)
.post(Entity.entity(getAccountInfoInputBean, MediaType.APPLICATION_JSON));
在Jersey 2.23和更高版本中, LoggingFeature
将为您解决问题。 它必须注册如下:
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT,
LoggingFeature.Verbosity.PAYLOAD_ANY);
Client client = ClientBuilder.newBuilder(clientConfig);
使用默认的详细程度设置( LoggingFeature.Verbosity.PAYLOAD_TEXT
),将记录请求和响应标头以及实体(如果认为是可读文本) 。 如果媒体类型是text/*
或以下之一,则该实体被视为可读文本:
application/atom+xml
application/json
application/svg+xml
application/x-www-form-urlencoded
application/xhtml+xml
application/xml
请注意,实体已登录到指定的最大字节数(请参阅LoggingFeature.LOGGING_FEATURE_MAX_ENTITY_SIZE
)。
在旧版本中,使用LoggingFilter
。 根据文档,自Jersey 2.23及更高版本开始,该过滤器已被弃用,并将很快被删除。
您可以使用LoggingFilter登录到System.out。
client.addFilter(new LoggingFilter(System.out));
WebTarget target = client.target(url);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.