- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。
public interface StreamingOutput {
void write(OutputStream output)
}
我不确定他们为什么要构建一个接口(interface)来公开响应输出流。为什么不直接注入(inject)一个 OutputStream,我们就可以简单地在上面写入!!
最佳答案
在书中RESTful Java with JAX-RS 2.0由 Bill Burk(RESTEasy 作者之一)撰写,您会找到关于 StreamingOutput
的很好的解释。
作者回答了你问的同样问题:
StreamingOutput
is a simple callback interface that you implement when you want to do raw streaming of response bodies [...]You allocate implemented instances of this interface and return them from your JAX-RS resource methods. When the JAX-RS runtime is ready to write the response body of the message, the
write()
method is invoked on theStreamingOutput
instance. [...]You may be asking yourself, “Why not just inject an
OutputStream
directly? Why have a callback object to do streaming output?” That’s a good question! The reason for having a callback object is that it gives the JAX-RS implementation freedom to handle output however it wants. For performance reasons, it may sometimes be beneficial for the JAX-RS implementation to use a different thread other than the calling thread to output responses. More importantly, many JAX-RS implementations have an interceptor model that abstracts things out like automatic GZIP encoding or response caching. Streaming directly can usually bypass these architectural constructs. Finally, the Servlet 3.0 specification has introduced the idea of asynchronous responses. The callback model fits in very nicely with the idea of asynchronous HTTP within the Servlet 3.0 specification.
和StreamingOutput
documentation陈述如下:
This is a lightweight alternative to a MessageBodyWriter.
关于jax-rs - jax-rs 用例中的 StreamingOutput?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38632895/
我有一个安静的网络服务。我正在考虑从类中的另一个方法调用一个方法。是否可以?如果是这样,我如何读取下面示例中的流输出的内容?另外,我分别需要这两个函数,以便我可以返回二进制文件或显示文本文件的文本 @
我正在尝试将 StreamingOutput 转换为 Java 中的字符串,以将其传递给另一个方法。它生成字符串,但稍后会引发以下错误。 JAXRSUtils logMessageHandlerPro
我已经在我的 Jersey Resource 类中实现了流输出。 @GET @Path("xxxxx") @Produces(BulkConstants.TEXT_XML_MEDIA_TYPE})
我有两个关于 Jersey 中的 StreamingOutput 的问题: 1) 它是否已被 jax-rs 运行时缓冲?我见过一些在重写 write() 方法时从 OutputStream 对象创建
我在从 StreamingOutput 实现中抛出 WebApplicationException 时遇到困难。我希望下面的代码返回 501,但curl 报告 curl: (52) Empty rep
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。 public interface StreamingOutput {
我有一个类,它将视频作为 mp4 文件发送给用户(Http 请求/响应)我想用主要逻辑模拟方法来测试它。我的代码 public StreamingOutput videoAsStream(final
有人可以发布一个示例,说明如何在 Jersey 中将 StreamingOutput 设置为 Response 对象中的实体吗? 我还没有找到这样的例子。 最佳答案 看看这是否有帮助: @GET @P
我们如何使用来自 AngularJS 的 get 请求来下载文件,并使用 REST API 到 Java 作为中间层,并且在 Java 层,数据根据 API 中的查询参数作为输入流。请帮忙 流程如下:
我在 Response 对象中返回一个 StreamingOutput: @GET @Path("/downloadFile/{filename}") @Produces(MediaType.TEXT
我的 ReSTLet 资源遇到问题。它使用 TrueZip 构建 Zip 存档的子集并允许用户下载它。 // Create a Streaming Response Entity. final
我在 spring boot 中创建了一个 web 应用程序,我想在其中使用 Spring MVC rest 框架而不是 jersey。我正在尝试做这样的事情,但它给了我错误。我想要 Spring M
我有一个 Jersey 服务将二进制数据输出为 StreamingOutput、MediaType.APPLICATION_OCTET_STREAM。 如何使用 Jersey 2 实现客户端来处理来自
我正在使用 Jersey StreamingOutput在我们升级到 Jersey 2.16 之前,它一直工作得很好。事情是这样的。我的 StreamingOuput在某些情况下产生输出非常缓慢。我确
我正在做类似于中提到的事情 Example of using StreamingOutput as Response entity in Jersey @GET @Produces(MediaType
StreamingOutput 的OutputStream 是否应该被实现类关闭? java-doc 没有给出任何建议。我猜它只是委托(delegate)给底层 ServletOutputStream
我的网络服务代码 final StreamingOutput stream = new StreamingOutput() { @Override public void write(fina
下面是我用来尝试流回 PDF 的代码。我之前在另一个环境中做过这件事,但由于某种原因,没有调用 StreamingOutput 上的 write() 方法。 没有错误被记录下来,它已经到了在日志中显示
我有一个返回StreamingOutput对象的方法。我想将这个 StreamingOutput 对象写入 Excel 文件。我编写了以下代码,这给了我 java.lang.ClassCastExce
我已使用此方法为我的 REST Web 服务创建一个 API,以便使用 jersey JAX-RS 下载文件: public Response returnFile(String filePath,S
我是一名优秀的程序员,十分优秀!