JERSEY : Error Trace : java.lang.IllegalStateException: Entity input stream has already been closed

时光毁灭记忆、已成空白 提交于 2019-12-06 04:06:38

I have had a similar requirement and the way I've worked around this issue was by kind of "cloning" the request entity, with a help of Apache Commons' IOUtils like the code snippet below:

//--- Create a new InputStream with the original entity ---//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(requestContext.getEntityStream(), baos);
InputStream originalEntity = new ByteArrayInputStream(baos.toByteArray()));

//--- Reasign the original entity to Request Context ---//
requestContext.setEntityStream(new ByteArrayInputStream(baos.toByteArray()));

//--- From this point onwards, you can do whatever you want with the original request entity ---//

. . .

//--- In my case, I am working with JsonObject as below (try/catch ommited for brevity) ---//
JsonReader jsonReader = Json.createReader(originalEntity);
JsonObject requestEntity = jsonReader.readObject();

. . .
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!