我需要用图像 jpeg 数据来响应
@Controller
@RequestMapping("/resource")
class ResourceController() {
@GetMapping("/thumbnail/{userFileId}/{group}/{index}", produces = [MediaType.IMAGE_JPEG_VALUE])
suspend fun getThumbnail(
@PathVariable userFileId: String,
@PathVariable group: String,
@PathVariable index: Int,
response: ServerHttpResponse
) {
val url = service.getThumbnailUrl(userFileId, group, index)
?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
response.headers.contentType = MediaType.IMAGE_JPEG
response.statusCode = HttpStatus.OK
streamResponse(url, response)
}
}
@Throws(ResponseStatusException::class)
private fun streamResponse(url: String, response: ServerHttpResponse) {
....
response.headers.contentLength = contentLength
content.use { content ->
response.writeWith(
DataBufferUtils.readInputStream({ content }, response.bufferFactory(), 128)
).subscribe()
}
}
它可以工作,但控制台中出现错误:
Could not resolve view with name 'resource/thumbnail/xxx/common/2'
同样的方法适用于spring-boot-starter-web.
不幸的是,我找不到如何使用 spring boot + kotlin + webflux 的文档