Subodh Joshi
Subodh Joshi

Reputation: 13502

Resteasy Client java.lang.IllegalStateException: Response is closed

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?

Upvotes: 12

Views: 16219

Answers (2)

Tiago Fischer
Tiago Fischer

Reputation: 145

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

response.bufferEntity();

Upvotes: 5

paprika
paprika

Reputation: 667

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.

Upvotes: 11

Related Questions