【发布时间】: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
当我尝试访问缓冲区时)
【问题讨论】: