简体   繁体   English

如何使用WebClient反应式Web客户端发送带有zip正文的POST请求

[英]How can I send POST request with zip body using WebClient reactive web client

How to POST a request with ZIP (compressed data) body using WebClient reactive web Client. 如何使用WebClient反应式Web客户端发布带有ZIP(压缩数据)正文的请求。

I created a zip in memory (bytearrayoutputstream) and want to send the zip data in a POST request body using WebClient reactive web Client. 我在内存(bytearrayoutputstream)中创建了一个zip,并希望使用WebClient反应式Web客户端在POST请求正文中发送zip数据。 The REST API is a PdfReactor REST WebService. REST API是PdfReactor REST Web服务。 The Response is a binary data (pdf). 响应是二进制数据(pdf)。

When I debug my Code I get the following error: Pooled connection observed an error 当我调试代码时,出现以下错误:池连接观察到错误

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported for bodyType=java.io.ByteArrayInputStream
    at org.springframework.web.reactive.function.BodyInserters.unsupportedError(BodyInserters.java:297) ~[spring-webflux-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.reactive.function.BodyInserters.lambda$writeWithMessageWriters$9(BodyInserters.java:287) ~[spring-webflux-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at java.base/java.util.Optional.orElseGet(Optional.java:358) ~[na:na]
    at org.springframework.web.reactive.function.BodyInserters.writeWithMessageWriters(BodyInserters.java:287) ~[spring-webflux-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.reactive.function.BodyInserters.lambda$fromObject$1(BodyInserters.java:85) ~[spring-webflux-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.reactive.function.client.DefaultClientRequestBuilder$BodyInserterRequest.writeTo(DefaultClientRequestBuilder.java:257) ~[spring-webflux-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$exchange$1(ExchangeFunctions.java:103) ~[spring-webflux-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.http.client.reactive.ReactorClientHttpConnector.lambda$connect$2(ReactorClientHttpConnector.java:110) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at reactor.netty.http.client.HttpClientConnect$HttpClientHandler.requestWithBody(HttpClientConnect.java:528) ~[reactor-netty-0.8.2.RELEASE.jar:0.8.2.RELEASE]
    at reactor.netty.http.client.HttpClientConnect$HttpObserver.lambda$onStateChange$0(HttpClientConnect.java:396) [reactor-netty-0.8.2.RELEASE.jar:0.8.2.RELEASE]
    at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44) ~[reactor-core-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at reactor.core.publisher.MonoOnAssembly.subscribe(MonoOnAssembly.java:76) [reactor-core-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at reactor.netty.http.client.HttpClientConnect$HttpObserver.onStateChange(HttpClientConnect.java:397) [reactor-netty-0.8.2.RELEASE.jar:0.8.2.RELEASE]
    at reactor.netty.resources.PooledConnectionProvider$DisposableAcquire.onStateChange(PooledConnectionProvider.java:501) [reactor-netty-0.8.2.RELEASE.jar:0.8.2.RELEASE]
    at reactor.netty.resources.PooledConnectionProvider$DisposableAcquire.run(PooledConnectionProvider.java:531) [reactor-netty-0.8.2.RELEASE.jar:0.8.2.RELEASE]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) [netty-common-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) [netty-common-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446) [netty-transport-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) [netty-common-4.1.29.Final.jar:4.1.29.Final]
    at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
    |_  Mono.error ⇢ org.springframework.web.reactive.function.BodyInserters.lambda$writeWithMessageWriters$9(BodyInserters.java:287)
    |_  Mono.defer ⇢ reactor.netty.http.client.HttpClientConnect$HttpObserver.onStateChange(HttpClientConnect.java:396)
 configuration.json:
    {
    "document": "test.html",
    "addBookmarks": true,
    "addLinks": true
    }

 test.html:
    <html>
     <img src="img/image.png" />
     <div>
    Hallo, hier ist ein Testdokument von <span th:text="${userName}" />.
     </div>
    </html>
map.put(template, tempBytesArray);
map.put(configuration, configByteArray);

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            try (ZipOutputStream zos_ = new ZipOutputStream(bos)) {
                map.forEach((k, v) -> {
                    var zipentry = new ZipEntry(k);
                    try {
                        zos_.putNextEntry(zipentry);
                        zos_.write(v);
                        zos_.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
               byte[] b = bos.toByteArray();
               BodyInserter<Object, ReactiveHttpOutputMessage>     bodyInserter = BodyInserters.fromObject(bos);
               byteArray = pdfReactorClient
                        .post()
                        .uri(pdfReactorUrl + PDF_REACTOR_URL_SUFFIX)
                        .accept(MediaType.APPLICATION_OCTET_STREAM)
                        .body(BodyInserters.fromObject(bos)
                        .exchange()
                        .flatMap(response -> response.bodyToMono (ByteArrayResource.class))
                        .map(ByteArrayResource::getByteArray)
                        .block();
                zos_.close();
            } catch (Exception e) {
                // TODO: handle exception
            }

If I execute the following CURL on command line, I get the result pdf: 如果我在命令行上执行以下CURL,则会得到结果pdf:

curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/zip" --data-binary @test.gzip "http://localhost:8080/service/rest/convert.pdf" > result.pdf . curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/zip" --data-binary @test.gzip "http://localhost:8080/service/rest/convert.pdf" > result.pdf

Same should be translated in Java reactive Code. 相同的内容应翻译成Java反应性代码。 The request body (Zip/asset package) to the PDFReactor is the problem. 问题是PDFReactor的请求正文 (Zip / asset包)。 Kindly help please... 请帮助...

PDFReactor PDF反应器

Content-Type for PDF is MediaType.APPLICATION_PDF PDF的内容类型为MediaType。APPLICATION_PDF

change the content type as shown below 更改内容类型,如下所示

                    pdfReactorClient
                        .post()
                        .uri(pdfReactorUrl + PDF_REACTOR_URL_SUFFIX)
                        .accept(MediaType.APPLICATION_PDF)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在发布请求的正文中发送文件? groovy - How i can send file in body of the post request? groovy 如何使用 Apache http 客户端的 URIBuilder 在 HTTP 请求中发送正文? - How can I send a body in a HTTP request using Apache http client's URIBuilder? 使用 WEBCLIENT 使用 POST 请求客户端方法 - to consume POST request client method using WEBCLIENT Spring WebClient 在使用 ExchangeFilterFunction 发送之前无法读取请求正文 - Spring WebClient can't read request body before send with ExchangeFilterFunction Spring Reactive Web Applications POST请求正文丢失 - Spring Reactive Web Applications POST Request body is missing 使用 WebClient 时对 POST 请求正文进行单元测试 - Unit Testing POST request body while using WebClient 如何使用Apache CXF WebClient在请求正文中发送JSON数据? - How to send JSON data in request body suing apache CXF webclient? 如何使用Jetty API客户端发送JSON请求? - How can I send a JSON request using Jetty API Client? 在 webclient 中 POST 请求以在 JSON 中添加请求正文 - POST request in webclient to add a request body in JSON 如何使用 Jersey POST 请求发送标头和正文? - How to send headers and body using Jersey POST request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM