为什么 JAX-RS Response.fromResponse(anotherResponse) 不复制实体?
Why JAX-RS Response.fromResponse(anotherResponse) not copying the entity?
这是 JAX-RS 服务器的一部分,它从另一个服务器接收响应并将相同的响应发送回其客户端。
这会将实体从 anotherResponse 复制到 responseForClient:
Response responseForClient = Response.fromResponse(anotherResponse).entity(anotherResponse.readEntity(InputStream.class)).build();
这不会复制实体:
Response responseForClient = Response.fromResponse(anotherResponse).build();
第二个也应该作为 JAX-RS Response.fromResponse() 也应该复制实体。
为什么需要设置实体?
我正在使用 RestEasy-3.0。
您必须在调用 fromResponse
之前使用 InputStream,因为它只会复制响应。 JAX-RS 不会自动执行此操作,如果您向客户端提供新实例,则不会使用该实体
请参阅 fromResponse
的文档
public static Response.ResponseBuilder fromResponse(Response response)
Create a new ResponseBuilder by performing a shallow copy of an existing Response.
The returned builder has its own response headers but the header values are shared with the original Response instance. The original response entity instance reference is set in the new response builder.
Note that if the entity is backed by an un-consumed input stream, the reference to the stream is copied. In such case make sure to buffer the entity stream of the original response instance before passing it to this method.
将读取 InputStream 的响应缓冲到字节数组
InputStream is = anotherResponse.readEntity(InputStream.class);
byte[] bytes = IOUtils.toByteArray(is);
ByteArrayInputStream in= new ByteArrayInputStream (bytes);
此代码与您的相同
Response responseForClient =
Response.fromResponse(anotherResponse).entity(in).build()
这是 JAX-RS 服务器的一部分,它从另一个服务器接收响应并将相同的响应发送回其客户端。
这会将实体从 anotherResponse 复制到 responseForClient:
Response responseForClient = Response.fromResponse(anotherResponse).entity(anotherResponse.readEntity(InputStream.class)).build();
这不会复制实体:
Response responseForClient = Response.fromResponse(anotherResponse).build();
第二个也应该作为 JAX-RS Response.fromResponse() 也应该复制实体。
为什么需要设置实体?
我正在使用 RestEasy-3.0。
您必须在调用 fromResponse
之前使用 InputStream,因为它只会复制响应。 JAX-RS 不会自动执行此操作,如果您向客户端提供新实例,则不会使用该实体
请参阅 fromResponse
public static Response.ResponseBuilder fromResponse(Response response)
Create a new ResponseBuilder by performing a shallow copy of an existing Response. The returned builder has its own response headers but the header values are shared with the original Response instance. The original response entity instance reference is set in the new response builder.
Note that if the entity is backed by an un-consumed input stream, the reference to the stream is copied. In such case make sure to buffer the entity stream of the original response instance before passing it to this method.
将读取 InputStream 的响应缓冲到字节数组
InputStream is = anotherResponse.readEntity(InputStream.class);
byte[] bytes = IOUtils.toByteArray(is);
ByteArrayInputStream in= new ByteArrayInputStream (bytes);
此代码与您的相同
Response responseForClient =
Response.fromResponse(anotherResponse).entity(in).build()