作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法使用 webClient (webflux) 有效使用 pdf Rest Web 服务
这是我的 webClient 创建:
ExchangeStrategies pdfExchangeStrategy = ExchangeStrategies
.builder()
.codecs(
clientCodecConfigurer -> {
CustomCodecs customCodecs = clientCodecConfigurer.customCodecs();
final ByteArrayDecoder byteArrayDecoder = new ByteArrayDecoder(){
@Override
public List<MimeType> getDecodableMimeTypes() {
return Collections.singletonList(APPLICATION_PDF);
}
};
customCodecs.decoder(byteArrayDecoder);
customCodecs.encoder(new ByteArrayEncoder());
DecoderHttpMessageReader pdfReader = new DecoderHttpMessageReader(byteArrayDecoder);
customCodecs.reader(pdfReader);
}
)
.build();
this.webClient = webClientFactory
.newBuilder(logger, "My web client")
.exchangeStrategies(pdfExchangeStrategy)
.defaultHeader(ACCEPT, APPLICATION_PDF_VALUE)
.defaultHeader(CONTENT_TYPE, APPLICATION_PDF_VALUE)
.baseUrl(this.baseUrl)
.build();
这是我的电话:
webClient.get()
.uri("http://localhost:8084/my-app/document/{id}", id)
.accept(APPLICATION_PDF)
.retrieve()
.bodyToMono(Byte[].class)
.block();
我收到此错误:
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/pdf' not supported
即使在supportedMediaTypes中,我有application/pdf
使用的网络服务是:
@GetMapping(value = "/document/{id}", produces = APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getDocument(@PathVariable String id) throws IOException {
LOGGER.info("get document with id = {}", id);
byte[] pdf = getInvoicePdf("document/sample.pdf");
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("filename", id + ".pdf");
headers.setContentType(APPLICATION_PDF);
headers.setContentLength(pdf.length);
return ResponseEntity
.ok()
.headers(headers)
.body(pdf);
}
感谢您的帮助
最佳答案
最终不需要所有样板交换策略,解决这个问题所需的只是:
webClient.get()
.uri("http://localhost:8084/my-app/document/{id}", id)
.accept(APPLICATION_PDF)
.exchange()
.block()
.bodyToMono(byte[].class)
.block()
关于java - 如何将pdf/byte[]消息阅读器添加到exchangeStrategies,不支持内容类型 'application/pdf',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53529224/
我使用以下代码(来自 this answer )来配置要记录到 WebClient 请求的 header : ExchangeStrategies exchangeStrategies = Excha
我是一名优秀的程序员,十分优秀!