Some calls must use the corporate proxy while another service is reached directly. Give those routes distinct RestTemplate instances instead of mutating one shared factory while requests can overlap.

Inside an existing Spring configuration class:

@Bean("directRestTemplate")
RestTemplate directRestTemplate() {
    var factory = new SimpleClientHttpRequestFactory();
    factory.setProxy(java.net.Proxy.NO_PROXY);
    return new RestTemplate(factory);
}

@Bean("proxyRestTemplate")
RestTemplate proxyRestTemplate() {
    var factory = new SimpleClientHttpRequestFactory();
    factory.setProxy(new java.net.Proxy(
        java.net.Proxy.Type.HTTP,
        new java.net.InetSocketAddress("proxy.example", 8080)));
    return new RestTemplate(factory);
}

For Spring 6.2's JDK request factory, Proxy.NO_PROXY supplies an explicit direct connection policy. Leaving the factory's proxy unset instead permits the ordinary URLConnection configuration path, which may select a JVM proxy.

Use @Qualifier on the injection point selecting the required bean, for example @Qualifier("directRestTemplate") RestTemplate rest. Merely defining the two beans does not choose between them for an ambiguous dependency.

This example selects an unauthenticated HTTP proxy. A transport needing proxy authentication can be constructed separately with its supported credential provider while retaining the same bean-selection arrangement. For Spring 6 with Apache HttpClient 5, scope that provider to the proxy endpoint.

Other interceptors or request factories deliberately installed by the application still belong to their own client configuration. Keep them with the route that actually needs them.

Erthyglow kevrennek