gpt4 book ai didi

java - 具有 Response 参数的方法内出现 IllegalStateException

转载 作者:行者123 更新时间:2023-12-02 00:11:05 25 4
gpt4 key购买 nike

我编写了一个简单的类来测试响应读取实体方法(如果它按我的预期工作)。但效果并不好。

当我启动我的类(class)时,我在 response.readEntity() 处收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: Method not supported on an outbound message.  
at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:150)

这是我编写的代码

public static void main(String[] args) {
List<Entity> representations = new ArrayList<>();
representations.add(new Entity("foo", "baz", false));
representations.add(new Entity("foo1", "baz1", true));
representations.add(new Entity("foo2", "baz2", false));
Response build = Response.ok(representations).build();
printEntitesFromResponse(build);
}

public static void printEntitesFromResponse(Response response) {
response
.readEntity(new GenericType<List<Entity>>() {})
.stream()
.forEach(entity -> System.out.println(entity));
}

我做错了什么?

最佳答案

有两种类型的响应:入站和出站,尽管它们仍然使用相同的接口(interface)。出站是指您从服务器端发送响应

Response response = Response.ok(entity).build();

入站是指您在客户端接收响应。

Response response = webTarget.request().get();

服务器端出站响应中禁用了 readEntity() 方法,因为您不需要它。仅当您需要对响应流中的响应进行反序列化时才使用它。但出站时就没有了。

如果您想要出站响应上的实体,只需使用 Response#getEntity()

关于java - 具有 Response 参数的方法内出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58121374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com