问题
I am doing some testing with Spring WebClient. In the following codes, I have set the compress to true. However when I check the debug logs, I can see the "accept-encoding: gzip" header is added, but the body is not compressed. Any way to force compress on post request body? Thanks.
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();
回答1:
Please refer to this.
.compress(true)
is for the server to response as gzip. That's why your post body is not compressed.
I am not familiar with manual http-client and web-client. When I use Spring, I use RestTemplate. This answer details how to force zipping your request body.
来源:https://stackoverflow.com/questions/57568018/how-to-compress-the-body-of-spring-webclient-post-request