我是Spring 5和Reactive Programming的新手。我的问题是通过rest API为数据库创建导出功能。
用户点击GET请求->服务器读取数据并以zip文件形式返回数据。由于zip文件很大,因此我需要流式传输这些数据。
我的代码如下:
@GetMapping(
value = "/export",
produces = ["application/octet-stream"],
headers = [
"Content-Disposition: attachment; filename=\"result.zip\"",
"Content-Type: application/zip"])
fun streamData(): Flux<Resource> = service.export()
我使用curl如下:
curl http://localhost/export -H "Accept: application/octet-stream"
但是它总是返回406 Not Acceptable 。
有人帮忙吗?
非常感谢
最佳答案
headers
批注的@GetMapping
属性不是应写入HTTP响应的 header ,而是映射 header 。这意味着您的@GetMapping
批注需要HTTP请求包含您列出的 header 。这就是为什么该请求实际上未映射到您的 Controller 处理程序的原因。
现在,您的处理程序返回类型看起来不正确-Flux<Resource>
意味着您打算返回0..*
Resource
实例,并且应该对其进行序列化。在这种情况下,像ResponseEntity<Resource>
这样的返回类型可能是一个更好的选择,因为您将能够在ResponseEntity
上设置响应 header ,并使用Resource
设置其主体。
关于stream - 使用WebFlux的响应zip文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414894/