Search code examples
javaresteasy

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?


Solution

  • 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.