gpt4 book ai didi

java - 如何将pdf/byte[]消息阅读器添加到exchangeStrategies,不支持内容类型 'application/pdf'

转载 作者:行者123 更新时间:2023-12-02 10:32:33 29 4
gpt4 key购买 nike

我无法使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com