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


- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01