Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short tutorial, we’ll take a look at how to send a request to a proxy using RestTemplate.

2. Dependencies

First, the RestTemplateCustomizer uses the HttpClient class to connect to the proxy.

To use the class, we need to add Apache’s httpcore dependency to our Maven pom.xml file:

<dependency>
    <groupId>org.apache.httpcomponents.core5</groupId>
    <artifactId>httpcore5</artifactId>
    <version>5.2.1</version>
</dependency>

Or to our Gradle build.gradle file:

compile 'org.apache.httpcomponents.core5:httpcore5:5.2.1'

3. Using SimpleClientHttpRequestFactory

Sending a request to a proxy using RestTemplate is pretty simple. All we need to do is to call the setProxy(java.net.Proxy) from SimpleClientHttpRequestFactory before building the RestTemplate object.

First, we start by configuring the SimpleClientHttpRequestFactory:

Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(PROXY_SERVER_HOST, PROXY_SERVER_PORT));
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setProxy(proxy);

Then, we move forward to passing the request factory instance to the RestTemplate constructor:

RestTemplate restTemplate = new RestTemplate(requestFactory);

Finally, once we have built the RestTemplate, we can use it to make proxied requests:

ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);

assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));

4. Using RestTemplateCustomizer

Another approach is to use a RestTemplateCustomizer with RestTemplateBuilder to build a customized RestTemplate.

Let’s start defining a ProxyCustomizer:

class ProxyCustomizer implements RestTemplateCustomizer {

    @Override
    public void customize(RestTemplate restTemplate) {
        HttpHost proxy = new HttpHost(PROXY_SERVER_HOST, PROXY_SERVER_PORT);
        HttpClient httpClient = HttpClientBuilder.create()
            .setRoutePlanner(new DefaultProxyRoutePlanner(proxy))
            .build();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
    }
}

After that, we build our customized RestTemplate:

RestTemplate restTemplate = new RestTemplateBuilder(new ProxyCustomizer()).build();

And finally, we use the RestTemplate to make requests that pass first through a proxy:

ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));

5. Conclusion

In this short tutorial, we’ve explored two different ways to send a request to a proxy using RestTemplate.

First, we learned how to send the request through a RestTemplate built using a SimpleClientHttpRequestFactory. Then we learned how to do the same using a RestTemplateCustomizer, this one being the recommended approach according to the documentation.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW