【发布时间】:2021-10-11 23:25:35
【问题描述】:
我目前正在使用 Spring WebFlux 尝试构建一个异步端点,该端点通过 Web 客户端从第三方端点获取 PDF,然后将 PDF 返回给我们的 API 使用者。但是,由于以下异常,我正在努力返回内容类型为application/pdf 的Mono<ResponseEntity>:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class reactor.core.publisher.MonoMapFuseable] with preset Content-Type 'application/pdf']
这里是控制器的实现。我的问题是:
- 我的实现方向是否正确,还是需要创建某种转换器?
-
Mono<ResponseEntity>是否甚至支持返回 PDF 作为响应正文?
@RequestMapping(value="/get-pdf", method = RequestMethod.GET)
public Mono<ResponseEntity> getPDFAsync() {
String url = "http://some-end-point";
WebClient client = WebClient.create(url);
return client.get()
.accept(MediaType.APPLICATION_PDF)
.exchangeToMono(response ->
Mono.just(ResponseEntity.ok().contentType(MediaType.APPLICATION_PDF)
.body(response.bodyToMono(ByteArrayResource.class)
.map(byteArrayResource -> byteArrayResource.getByteArray())
)));
}
【问题讨论】:
-
这能回答你的问题吗? Return generated pdf using spring MVC
-
@Toerktumlare 这不是一种非常被动的方法,请参阅我的答案以获得一种可能的解决方案。
-
我并没有声称它是一个完整的服务答案,但它为您提供了如何完成的指南,然后您可以将其调整为响应式。例如,您在我发布的链接中查看的第二个问题,它们传输字节数组,而 webflux 支持传输字节流。
标签: spring-boot spring-webflux project-reactor reactor spring-webclient