Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resteasy Client java.lang.IllegalStateException: Response is closed

Tags:

java

resteasy

I am getting below exception in RestEasy client -3.0.8

12:46:19,724 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) java.lang.IllegalStateException: Response is closed.

I have write below code

client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(request.getUrl());
Response response = target.request().accept(APPLICATION_TYPE_XML).header(TOKEN, request.getToken()).post(Entity.entity(request.getXmlObject(), APPLICATION_TYPE_XML));
output = response.readEntity(String.class);
if (response.getStatus() != SUCCESS_CREATE) {
 //Do Something
} else {
 String classType = ClassFactory.getClassNameFromUrl(request.getUrl());
 if (null != classType && !classType.isEmpty()) {
  Long Id = (Long) response.readEntity(ClassFactory.getClassMethod(classType)).getId();

 }

Now this line Long Id = (Long) response.readEntity(ClassFactory.getClassMethod(classType)).getId(); throwing exception . Whats wrong with the code?

like image 815
Subodh Joshi Avatar asked Feb 24 '16 07:02

Subodh Joshi


2 Answers

Reading the response closes the response. So when you call response.readEntity(String.class); this will lead to an error when you repeat the read with response.readEntity(ClassFactory.getClassMethod(classType)).getId();

You can show this easily by repeating the first readEntity in a loop. Read the response once into the most convenient form and convert it if necessary from there.

like image 181
paprika Avatar answered Sep 24 '22 15:09

paprika


You can buffer the body response to allow multiple calls to readEntity():

response.bufferEntity();
like image 38
Tiago Fischer Avatar answered Sep 24 '22 15:09

Tiago Fischer