Quarkus - 反应式文件下载

Quarkus - Reactive file download

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

到目前为止,我尝试 return 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>
}

尝试 return 服务器端的 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(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)
    ...

Here 是一个如何使用 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;
}

您可以在本地使用 mvn quarkus:dev 进行测试,然后转到此 url 查看那里有哪些文件 http://localhost:8080/hello/list/test and after that you can call this url to start download http://localhost:8080/hello/f/reactive-file-download-dev.jar

我没有检查 Flux(看起来 spring 然后是 quarkus),请随时分享您的想法。我只是在学习 answering/sharing.