IllegalStateException内的方法与响应参数

问题描述:

我写了一个简单的类来测试响应阅读实体方法(如果它按预期工作)。但它运作不好。IllegalStateException内的方法与响应参数

当我启动我的课堂我得到的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)); 
    } 

我在做什么错误的代码?

实现有两种类型的Response ES,入站和出站,虽然他们仍然使用相同的接口。出站是当您从服务器发送响应时

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

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

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

readEntity方法在服务器端出站响应中被禁用,因为您不需要它。它仅在您需要_de_序列化来自响应流的响应时使用。但是当它出站时没有。

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

这方法是如何在glassfish

@Override 
149  public <T> T readEntity(GenericType<T> entityType) throws ProcessingException { 
150   throw new IllegalStateException(LocalizationMessages.NOT_SUPPORTED_ON_OUTBOUND_MESSAGE()); 
151  }