RestTemplate + OkHttp3 - transparent processing of gzip'ed response?

Viewed 241

I have been looking for how to enable transparent processing of gzip'ed response using RestTemplate with OkHttp3 set as its http client.

Below is how I define the bean for RestTemplate:

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {

        RestTemplate restTemplate = restTemplateBuilder //
                .rootUri(_endpoint) //
                .additionalInterceptors(new ClientHttpRequestInterceptor() {

                    @Override
                    public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                            ClientHttpRequestExecution execution) throws IOException {

                        request.getHeaders().add(HttpHeaders.ACCEPT_ENCODING, "gzip");

                        ClientHttpResponse response = execution.execute(request, body);

                        return response; // if I set a breakpoint here, I
                                         // can see it's OkHttp3ClientHttpResponse
                    }
                }) //
                .build();

        return restTemplate;
    }

In my pom.xml I have:

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>

Sure there is a solution to enable gzip response processing. But to me that's no transparent.

There is a solution for RestTemplate + Apache HttpClient which I only need to switch the pom.xml dependency to

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

It works with the same Bean code above. That's what I call transparent. However due to a number of reasons I had to use OkHttp3.

I've looked at the official doc of OkHttp3 but it doesn't give a specific instruction. This is ironic, because if it's truly transparent I shouldn't need to do anything.

If anyone knows any details, please help. Appreicated!

1 Answers
Related