带有响应参数的方法中的IllegalStateException

VanDavv:

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

当我启动课程时,出现以下错误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));
    }

我究竟做错了什么?

保罗·萨姆索塔(Paul Samsotha):

Responsees 有两种类型,入站和出站,尽管它们仍使用相同的接口。出站是从服务器发送响应时

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

入站是指您在客户端上收到响应时。

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

readEntity在服务器端出站响应中禁用了方法,因为您不需要它。仅在需要对响应流的响应进行_de_serialize时使用。但是出站时没有任何内容。

如果您希望实体出站响应,只需使用 Response#getEntity()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章