带有响应参数的方法中的 IllegalStateException [英] IllegalStateException within method with Response paramether

查看:13
本文介绍了带有响应参数的方法中的 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 Responses, 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆