Quarkus - 反应式文件下载

Mar*_*lze 5 http download kotlin reactive quarkus

使用Quarkus,有人可以举一个例子,说明服务器和客户端代码如何使用反应式 API 通过 http 下载文件?

到目前为止,我尝试返回 nio ByteBuffers 的 Flux 但似乎不支持:

@RegisterRestClient(baseUri = "http://some-page.com")
interface SomeService {

    // same interface for client and server
    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    @Path("/somePath")
    fun downloadFile(): reactor.core.publisher.Flux<java.nio.ByteBuffer>
}
Run Code Online (Sandbox Code Playgroud)

尝试在服务器端返回 Flux 会导致以下异常:

ERROR: RESTEASY002005: Failed executing GET /somePath
org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: kotlinx.coroutines.reactor.FlowAsFlux of media type: application/octet-stream
    at org.jboss.resteasy.core.ServerResponseWriter.lambda$writeNomapResponse$3(ServerResponseWriter.java:124)
    at org.jboss.resteasy.core.interception.jaxrs.ContainerResponseContextImpl.filter(ContainerResponseContextImpl.java:403)
    at org.jboss.resteasy.core.ServerResponseWriter.executeFilters(ServerResponseWriter.java:251)
    ...
Run Code Online (Sandbox Code Playgroud)

özk*_*dil 8

以下是如何使用 Smallrye Mutiny 启动反应式文件下载的示例。主要函数是getFile

@GET
@Path("/f/{fileName}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Uni<Response> getFile(@PathParam String fileName) {
    File nf = new File(fileName);
    log.info("file:" + nf.exists());
    ResponseBuilder response = Response.ok((Object) nf);
    response.header("Content-Disposition", "attachment;filename=" + nf);
    Uni<Response> re = Uni.createFrom().item(response.build());
    return re;
}
Run Code Online (Sandbox Code Playgroud)

您可以在本地进行测试mvn quarkus:dev,然后转到此网址查看其中有哪些文件http://localhost:8080/hello/list/test,然后您可以调用此网址开始下载http://localhost:8080/你好/f/reactive-file-download-dev.jar

我没有检查 Flux(它看起来更像 spring,然后是 quarkus),请随意分享您的想法。我只是在学习和回答/分享。

  • 我必须承认我的问题现在不再真正明智,因为反应堆(Flux、Mono)已在 Quarkus 内部被弃用,转而支持叛变(Multi、Uni)。非常感谢示例项目! (2认同)