如何在响应中调用readEntity两次? [英] How to call readEntity on a Response twice?

查看:314
本文介绍了如何在响应中调用readEntity两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在做的是导致:

What I'm doing right now is resulting in a:

java.io.IOException: stream is closed

,因为它在第一次读取后关闭了流。

on the 2nd readEntity() since it closes the stream after the first read.

以下是我正在做的事情:

Here is what I'm doing:

Response response = target.queryParam("start", startIndex)
   .queryParam("end", end)
   .request()
   .accept(MediaType.APPLICATION_XML)
   .header(authorizationHeaderName, authorizationHeaderValue)
   .get();

String xml = response.readEntity(String.class);
ourLogger.debug(xml);


MyClass message = response.readEntity(MyClass.class); //throws IOException


推荐答案

/你可以使用 Response#bufferEntity () ,这将允许您多次读取实体流。

/You can use Response#bufferEntity(), which will allow you to read the entity stream multiple times.

Response response = ...
response.bufferEntity();
String s = response.readEntity(String.class);
MyEntity me = response.readEntity(MyEntity.class);
response.close();



更新



阅读实体后使用 readEntity(),读取的结果将被缓存,并且可以通过调用 getEntity()来获得。这些信息并没有真正回答OP的问题,但我认为这是有用的信息。

Update

After you read the entity with readEntity(), the result of the reading is cached and is available with the call to getEntity(). This information doesn't really answer the OP's question, but I thought it was useful information to add in.

这篇关于如何在响应中调用readEntity两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆