75. HTTP clients

75.1 Configure RestTemplate to use a proxy

As described in Section 33.1, “RestTemplate customization”, a RestTemplateCustomizer can be used with RestTemplateBuilder to build a customized RestTemplate. This is the recommended approach for creating a RestTemplate configured to use a proxy.

The exact details of the proxy configuration depend on the underlying client request factory that is being used. Here’s an example of configuring HttpComponentsClientRequestFactory with an HttpClient that uses a proxy for all hosts except

static class ProxyCustomizer implements RestTemplateCustomizer {

    public void customize(RestTemplate restTemplate) {
        HttpHost proxy = new HttpHost("proxy.example.com");
        HttpClient httpClient = HttpClientBuilder.create()
                .setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {

                    public HttpHost determineProxy(HttpHost target,
                            HttpRequest request, HttpContext context)
                                    throws HttpException {
                        if (target.getHostName().equals("")) {
                            return null;
                        return super.determineProxy(target, request, context);

                new HttpComponentsClientHttpRequestFactory(httpClient));