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

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

问题描述

我现在正在做的事情导致:

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

java.io.IOException: stream is closed

在第二次 readEntity() 上,因为它在第一次读取后关闭了流.

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

这是我正在做的:

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天全站免登陆