Van*_*avv 15 java jax-rs dropwizard
我写了一个简单的类来测试响应读取实体方法(如果它按预期工作).但它没有奏效.
当我启动课程时,我收到以下错误response.readEntity():
Run Code Online (Sandbox Code Playgroud)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));
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Pau*_*tha 29
Response虽然它们仍然使用相同的接口,但有两种类型的es,inbound和outbound.出站是指从服务器发送响应
Response response = Response.ok(entity).build();
Run Code Online (Sandbox Code Playgroud)
入站是在客户端收到响应时.
Response response = webTarget.request().get();
Run Code Online (Sandbox Code Playgroud)
readEntity在服务器端出站响应上禁用该方法,因为您不需要它.它仅在您需要_de_serialize响应流的响应时使用.但是当它出站时没有.
如果您希望实体出站响应,请使用 Response#getEntity()
| 归档时间: |
|
| 查看次数: |
10403 次 |
| 最近记录: |