Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何压缩Spring WebClient post请求的主体?_Spring Boot_Reactor Netty_Spring Webclient - Fatal编程技术网

Spring boot 如何压缩Spring WebClient post请求的主体?

Spring boot 如何压缩Spring WebClient post请求的主体?,spring-boot,reactor-netty,spring-webclient,Spring Boot,Reactor Netty,Spring Webclient,我正在用SpringWebClient做一些测试。在下面的代码中,我将compress设置为true。但是,当我检查调试日志时,我可以看到添加了“accept encoding:gzip”头,但是主体没有被压缩。有没有办法强制压缩请求后的正文?谢谢 HttpClient httpClient = HttpClient.create().compress(true).wiretap(true); WebClient webClient = WebClient.builder()

我正在用SpringWebClient做一些测试。在下面的代码中,我将compress设置为true。但是,当我检查调试日志时,我可以看到添加了“accept encoding:gzip”头,但是主体没有被压缩。有没有办法强制压缩请求后的正文?谢谢

HttpClient httpClient = HttpClient.create().compress(true).wiretap(true);

WebClient webClient = WebClient.builder()
                .baseUrl("https://jsonplaceholder.typicode.com")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();

Post post = new Post();

        post.setUserId(1000L);
        post.setId(2000L);
        post.setTitle("Reactor Netty");

        StringBuffer sb = new StringBuffer();
        IntStream.range(1, 1000).forEach(i -> sb.append("Spring boot webclient"));
        post.setBody(sb.toString());

        Post p = webClient.post().uri("/posts").syncBody(post).retrieve().bodyToMono(Post.class).block();
请参阅

.compress(true)
用于服务器响应为gzip。这就是为什么你的帖子没有被压缩


我不熟悉手动http客户端和web客户端。当我使用Spring时,我使用RestTemplate。详细说明如何强制压缩请求正文。

谢谢您的回答。你肯定可以用额外的代码来屏蔽身体。但是我认为这个普通任务可能是现成的。
compress(true)
用于服务器响应。对于请求主体,Netty默认不支持:@VioletaGeorgieva谢谢Violeta!你回答了我的问题。你想发送压缩后的请求正文还是接收压缩后的响应?双向。compress()方法给我的印象是这个任务由库负责。