Resteasy Client java.lang.IllegalStateException: Response is closed

删除回忆录丶 提交于 2019-12-10 13:19:17

问题


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?


回答1:


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.




回答2:


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

response.bufferEntity();



回答3:


Did you solved? I 've faced a similar problem.

Im running in WildFly 9 My REST Resource is defined as

@Path("fake")
public interface FakeService {

     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String get();
}

And the implementation

public class Fake implements FakeService{
    public String get(){
        return "Hello";
    }
}

And my client code is

ResteasyClient client = new ResteasyClientBuilder().build();
**ResteasyWebTarget target = client.target("http://localhost:8080/myapp/api/fake");**

FakeService simple = target.proxy(FakeService.class);
System.out.println(simple.get());

After looking my "server.log" I've found that the excepction is throwed because there is a warning before,

WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-24) failed to execute: javax.ws.rs.NotFoundException: Could not find resource for full path: **http://localhost:8080/myapp/api/fake/fake**

The URL add an extra path (fake is twice). I've changed my cliente code to ResteasyWebTarget target = client.target("http://localhost:8080/myapp/api/");

(without the path of the resource I am proxying) and it worked.



来源:https://stackoverflow.com/questions/35595707/resteasy-client-java-lang-illegalstateexception-response-is-closed

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