Java Jersey客户端日志响应和getEntity
我正在使用Jersey客户端拨打REST服务电话。现在,当我收到响应时,我希望记录json响应,并且我还希望在响应bean中填充实体Java Jersey客户端日志响应和getEntity,java,json,rest,jersey,jersey-client,Java,Json,Rest,Jersey,Jersey Client,我正在使用Jersey客户端拨打REST服务电话。现在,当我收到响应时,我希望记录json响应,并且我还希望在响应bean中填充实体 Client client = Client.create(config); ClientResponse cr = client .resource(endPoint) .accept("application/json") .get(Clie
Client client = Client.create(config);
ClientResponse cr = client
.resource(endPoint)
.accept("application/json")
.get(ClientResponse.class);
clientResponse.bufferEntity();
String responseToLog = cr.getEntity(String.class);
log.debug(responseToLog);
MyResponseBean myResponse = cr.getEntity(MyResponseBean.class);
现在的问题是,我们不能两次调用getEntity(),因为流第一次被使用,然后我们就不能第二次使用它。因此,上述代码给出了一个例外,即由于输入结束,没有要映射到对象的内容。我相信这不是一个非常独特的要求,而且会很普遍。那么,最好的做法或方法是什么呢?编辑
一个有效的例子:
String endPoint = "http://localhost:8080/api/places";
Client client = new Client();
ClientResponse cr = client.resource(endPoint).accept("application/json").get(ClientResponse.class);
cr.bufferEntity(); // buffer the entity (HttpInputStream->ByteArrayInputStream)
String res1 = cr.getEntity(String.class);
cr.getEntityInputStream().reset(); //reset the buffer, this is a ByteArrayInputStream
String res2 = cr.getEntity(String.class);//read again
参考:检查来源。您可以使用球衣。应该记录尸体。如果没有,还可以提供PrintStream来捕获输入。或者bufferEntity应该可以工作。我不知道。我已经有一段时间没有使用Jersey 1了,我可以尝试一下,但我不确定如何将LoggingFilter与我现有的logger连接起来。我会查一查。