我的代码的目的是从第三方服务检索图像。
为了让下载的端点工作,我有点吃力,但只成功了一部分。当我通过邮递员调用端点时,答案是一个.bin文件,但我需要的是一个.png文件。最大的成功是能够获得一个.png文件,并且能够自定义名称。但个性化并不是绝对必要的。
该项目使用初始值设定项构建,并具有以下依赖项:
- 春季启动web
- lombok
- 弹簧靴启动器webflux
- 反应堆弹簧
下面是我的端点的源代码:
@GetMapping("/retrieve-image")
public Mono<byte[]> retrieveImage(ImageRequest request) throws ExecutionException, InterruptedException, IOException {
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
queryParams.add("attribute", request.getAttribute()); // fake for query string setting.
Mono<byte[]> image = webClient
.get()
.uri(uriBuilder -> uriBuilder
.path(Endpoint.THIRD_PARTY_SERVICE_URI)
.queryParams(queryParams)
.build())
.accept(MediaType.valueOf(String.valueOf(MediaType.IMAGE_PNG)))
.exchange()
.flatMap(clientResponse -> clientResponse.bodyToMono(byte[].class)
.doOnSuccess(body -> {
if (clientResponse.statusCode().isError()) {
log.error("HttpStatusCode = {}", clientResponse.statusCode());
log.error("HttpHeaders = {}", clientResponse.headers().asHttpHeaders());
log.error("ResponseBody = {}", body);
}
}));
return image;
}
您还可以将文件的mime类型添加到@GetMapping
注释的products部分,它应该看起来像这样:
@GetMapping(path = "/retrieve-image",
produces = "image/png")
此外,您可以将响应封装在ResponseEntity<Resource>
中,而不是返回Mono<byte[]>
。这使您可以添加Headers并告诉浏览器您的文件名。例如:
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=image.png");
header.add("Access-Control-Expose-Headers", "Content-Disposition");
return ResponseEntity.ok().
.headers(header)
.contentLength(Files.size(path))
.body(<<YOUR_FILE_HERE>>);
最后一个想法是:如果您将spring-boot-starter-web和spring-boot-starter-webflux都添加到您的依赖项中,该应用程序将运行,但它不使用webflux中的Netty,而是使用通常的Tomcat。因此,您不会从被动功能中获益。