【问题标题】:Get headers from a Flowable response using the Micronaut Declarative Client使用 Micronaut 声明式客户端从 Flowable 响应中获取标头
【发布时间】:2019-12-11 23:00:24
【问题描述】:

我正在使用 Micronaut 声明式客户端在 grails 4 应用程序中使用后端 API。我已经成功实现了一些 GET/POST/PUT 和 DELETE 请求,但现在我想实现一个下载端点,这个端点返回一个沉重的 zip 文件,所以我尝试了:

@Get(value = '/download/{zipFileId}', processes = 'application/zip')
abstract Flowable<ByteBuffer<?>> downloadZip(Long zipFileId)

它有效,但我不知道如何从该响应中获取一些标头,例如“Content-Length”,所以,我的问题是,有一种方法可以使用 Micronaut 声明式客户端从 Flowable 响应中获取标头?

我尝试了低级客户端,它正在使用:

Flowable<HttpResponse<ByteBuffer<?>>> response = rxStreamingHttpClient.exchangeStream(request)
response.blockingSubscribe(new Subscriber<HttpResponse<ByteBuffer<?>>>() {
    int number = 0

    ...

    @Override
    void onNext(HttpResponse<ByteBuffer<?>> httpResponse) {
        ByteBuffer<?> buffer = byteBufferHttpResponse.body()
        if(number == 0) {
            httpServletResponse.setHeader('Content-Length', httpResponse.headers.get('Content-Length'))
        }
        IOUtils.copyLarge(buffer.toInputStream(), httpServletResponse.getOutputStream())
        ((ReferenceCounted) buffer).release()
        number++
        subscription.request(1)
    }

    ...

})

我在声明式客户端中尝试过

@Get(value = '/download/{zipFileId}', processes = 'application/zip')
Flowable<HttpResponse<ByteBuffer<?>>> downloadZip(Long zipFileId)

但看起来客户端在订阅者中调用 onNext 之前下载了所有文件(我得到了一个

io.netty.util.IllegalReferenceCountException: refCnt: 0

当我尝试访问缓冲区时)

【问题讨论】:

    标签: grails micronaut


    【解决方案1】:

    如果你想获取响应数据,还想流式传输内容,你可以这样做:

    HttpResponse&lt;Flowable&lt;ByteBuffer&lt;?&gt;&gt;&gt; downloadZip(Long zipFileId)

    响应正文将是一个可流动的,您可以订阅然后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多