IllegalStateException within method with Response paramether(带有响应参数的方法中的 IllegalStateException)
问题描述
我编写了一个简单的类来测试响应读取实体方法(如果它按我预期的那样工作).但效果并不好.
当我启动我的课程时,我在 response.readEntity()
处收到以下错误:
线程main"java.lang.IllegalStateException 中的异常:出站消息不支持该方法.在 org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:150)
这是我写的代码
public static void main(String[] args) {列出<实体>表示 = 新的 ArrayList<>();representations.add(new Entity("foo", "baz", false));表示.添加(新实体(foo1",baz1",真));表示.添加(新实体(foo2",baz2",假));响应构建 = Response.ok(representations).build();printEntitesFromResponse(build);}公共静态无效 printEntitesFromResponse(响应响应){回复.readEntity(new GenericType<List<Entity>>() {}).溪流().forEach(entity -> System.out.println(entity));}
我做错了什么?
Response
有两种类型,入站和出站,尽管它们仍然使用相同的接口.出站是当您从服务器端发送响应时
响应响应 = Response.ok(entity).build();
入站是指您在客户端收到响应.
响应响应 = webTarget.request().get();
readEntity()
方法在服务器端出站响应中被禁用,因为您不需要它.它仅在您需要de-序列化响应流中的响应时使用.但是出站时没有.
如果您想要出站响应中的实体,只需使用 Response#getEntity()
I wrote a simple class to test response reading entity method (if it works as I expect). But it didn't worked well.
When I launch my class I get following error at 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)
And here's the code I wrote
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));
}
What am I doing wrong?
There are two types of Response
s, inbound and outbound, though they still use the same interface. Outbound is when you are sending a response from the server-side
Response response = Response.ok(entity).build();
Inbound is when you are receiving the response on the client-side.
Response response = webTarget.request().get();
The readEntity()
method is disabled on the server-side outbound response because you don't need it. It's only used when you need to de-serialize the response from the response stream. But there is none when it's outbound.
If you want the entity on the outbound response, just use Response#getEntity()
这篇关于带有响应参数的方法中的 IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有响应参数的方法中的 IllegalStateException
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01