我正在 Kotlin 中制作一个小型 Spring WebFlux 应用程序的原型。该应用程序需要从远程 REST 端点获取 tar 存档并将其存储在本地磁盘上。听起来很简单。
我首先创建了一个集成测试,该测试启动 Spring 服务器和另一个 WebFlux 服务器,并使用一个为 tar 存档提供服务的模拟 REST 端点。
测试应该是这样的:
1)应用程序:获取mock-server/archive
2) 模拟服务器:响应状态为 200,正文中的 tar 存档作为类型附件
3)app:阻塞直到收到所有字节,然后解压并使用文件
我遇到的问题是,当我尝试将字节收集到ByteArray
在应用程序上,它会永远阻止。
My mock-server/archive
路由到以下函数:
fun serveArchive(request: ServerRequest): Mono<ServerResponse> {
val tarFile = FileSystemResource(ARCHIVE_PATH)
assert(tarFile.exists() && tarFile.isFile && tarFile.contentLength() != 0L)
return ServerResponse
.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(tarFile.contentLength())
.header("Content-Disposition", "attachment; filename=\"$ARCHIVE_FNAME\"")
.body(fromResource(tarFile))
}
然后我的应用程序使用以下内容调用它:
private fun retrieveArchive {
client.get().uri(ARCHIVE_URL).accept(MediaType.APPLICATION_OCTET_STREAM)
.exchange()
.flatMap { response ->
storeArchive(response.bodyToMono())
}.subscribe()
}
private fun storeArchive(archive: Mono<ByteArrayResource>): Mono<Void> {
val archiveContentBytes = archive.block() // <- this blocks forever
val archiveContents = TarArchiveInputStream(archiveContentBytes.inputStream)
// read archive
}
我见过如何最好地从 Spring WebClient 的 ClientResponse 获取字节数组? https://stackoverflow.com/questions/44821187/how-to-best-get-a-byte-array-from-a-clientresponse-from-spring-webclient这就是为什么我尝试使用ByteArrayResource
.
当我经历一切时,我看到serveArchive
似乎正在工作(断言语句表示我传递的文件存在并且其中有一些字节)。在retrieveArchive
我得到了 200 并且可以在.headers
(内容类型、内容长度看起来都不错)。当我静下心来storeArchive
并尝试使用以下命令从 Mono 检索字节block
,它只是永远阻塞。
我完全不知道如何调试这样的东西。