调用 REST 服务
Spring Boot 提供了多种便捷的方式来调用远程 REST 服务。如果你正在开发一个非阻塞的响应式应用程序,并且使用了 Spring WebFlux,那么你可以使用 WebClient。如果你更倾向于使用阻塞式 API,则可以使用 RestClient 或 RestTemplate。
WebClient
如果你没有编写响应式的 Spring WebFlux 应用程序,可以使用 RestClient 来替代 WebClient。它提供了类似的函数式 API,但它是阻塞式的,而非响应式的。
Spring Boot 会为你创建并预配置一个原型(prototype)的 WebClient.Builder bean。强烈建议你在组件中注入该 builder,并用它来创建 WebClient 实例。Spring Boot 会对该 builder 进行配置,使其共享 HTTP 资源,并以与服务器端相同的方式反映编解码器(codecs)的设置(参见 WebFlux HTTP 编解码器自动配置),以及其他功能。
以下代码展示了一个典型示例:
- Java
- Kotlin
import reactor.core.publisher.Mono;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class MyService {
private final WebClient webClient;
public MyService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://example.org").build();
}
public Mono<Details> someRestCall(String name) {
return this.webClient.get().uri("/{name}/details", name).retrieve().bodyToMono(Details.class);
}
}
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
@Service
class MyService(webClientBuilder: WebClient.Builder) {
private val webClient: WebClient
init {
webClient = webClientBuilder.baseUrl("https://example.org").build()
}
fun someRestCall(name: String?): Mono<Details> {
return webClient.get().uri("/{name}/details", name)
.retrieve().bodyToMono(Details::class.java)
}
}
WebClient Runtime
Spring Boot 会根据应用程序类路径(classpath)上可用的库,自动检测使用哪个 ClientHttpConnector 来驱动 WebClient。按优先级顺序,支持以下客户端:
-
Reactor Netty
-
Jetty RS client
-
Apache HttpClient
-
JDK HttpClient
如果 classpath 中存在多个客户端,则将使用优先级最高的客户端。
spring-boot-starter-webflux starter 默认依赖于 io.projectreactor.netty:reactor-netty,它同时提供了服务器和客户端的实现。如果你选择使用 Jetty 作为响应式服务器,则应添加 Jetty 响应式 HTTP 客户端库 org.eclipse.jetty:jetty-reactive-httpclient 的依赖。对服务器和客户端使用相同的技术具有一定的优势,因为它会自动在客户端和服务器之间共享 HTTP 资源。
开发者可以通过提供自定义的 ReactorResourceFactory 或 JettyResourceFactory Bean 来覆盖 Jetty 和 Reactor Netty 的资源配置——该配置将同时应用于客户端和服务器。
如果你想为客户端覆盖该选择,可以定义自己的 ClientHttpConnector Bean,从而完全控制客户端的配置。
你可以在 Spring Framework 参考文档 中了解更多关于 WebClient 配置选项的内容。
全局 HTTP 连接器配置
如果自动检测到的 ClientHttpConnector 无法满足你的需求,你可以使用 spring.http.reactiveclient.connector 属性来选择特定的连接器。例如,如果你的 classpath 中包含 Reactor Netty,但你更倾向于使用 Jetty 的 HttpClient,你可以添加以下内容:
- Properties
- YAML
spring.http.reactiveclient.connector=jetty
spring:
http:
reactiveclient:
connector: jetty
你也可以设置属性来更改将应用于所有响应式连接器的默认值。例如,你可能想要更改超时时间以及是否跟随重定向:
- Properties
- YAML
spring.http.reactiveclient.connect-timeout=2s
spring.http.reactiveclient.read-timeout=1s
spring.http.reactiveclient.redirects=dont-follow
spring:
http:
reactiveclient:
connect-timeout: 2s
read-timeout: 1s
redirects: dont-follow
对于更复杂的自定义需求,你可以使用 ClientHttpConnectorBuilderCustomizer,或者声明你自己的 ClientHttpConnectorBuilder bean,这将导致自动配置退避。当你需要自定义底层 HTTP 库的一些内部细节时,这种方式会很有用。
例如,以下代码将使用一个配置了特定 ProxySelector 的 JDK 客户端:
- Java
- Kotlin
import java.net.ProxySelector;
import org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyConnectorHttpConfiguration {
@Bean
ClientHttpConnectorBuilder<?> clientHttpConnectorBuilder(ProxySelector proxySelector) {
return ClientHttpConnectorBuilder.jdk().withHttpClientCustomizer((builder) -> builder.proxy(proxySelector));
}
}
import org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.net.ProxySelector
@Configuration(proxyBeanMethods = false)
class MyConnectorHttpConfiguration {
@Bean
fun clientHttpConnectorBuilder(proxySelector: ProxySelector): ClientHttpConnectorBuilder<*> {
return ClientHttpConnectorBuilder.jdk().withHttpClientCustomizer { builder -> builder.proxy(proxySelector) }
}
}
WebClient 自定义
有三种主要方法来定制 WebClient,具体取决于你希望定制的范围有多广。
为了使任何自定义的范围尽可能窄,请注入自动配置的 WebClient.Builder,然后按需调用其方法。WebClient.Builder 实例是有状态的:对该构建器的任何更改都会反映在后续使用它创建的所有客户端中。如果你想使用相同的构建器创建多个客户端,也可以考虑通过 WebClient.Builder other = builder.clone(); 来克隆该构建器。
要对所有 WebClient.Builder 实例进行应用范围的、可叠加的自定义,你可以声明 WebClientCustomizer Bean,并在注入点本地修改 WebClient.Builder。
最后,你可以回退到原始 API 并使用 WebClient.create()。在这种情况下,不会应用任何自动配置或 WebClientCustomizer。
WebClient SSL 支持
如果你需要在 WebClient 所使用的 ClientHttpConnector 上进行自定义 SSL 配置,可以注入一个 WebClientSsl 实例,并通过构建器的 apply 方法使用它。
WebClientSsl 接口提供了对你在 application.properties 或 application.yaml 文件中定义的任何 SSL bundles 的访问。
以下代码展示了一个典型示例:
- Java
- Kotlin
import reactor.core.publisher.Mono;
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientSsl;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class MyService {
private final WebClient webClient;
public MyService(WebClient.Builder webClientBuilder, WebClientSsl ssl) {
this.webClient = webClientBuilder.baseUrl("https://example.org").apply(ssl.fromBundle("mybundle")).build();
}
public Mono<Details> someRestCall(String name) {
return this.webClient.get().uri("/{name}/details", name).retrieve().bodyToMono(Details.class);
}
}
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientSsl
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
@Service
class MyService(webClientBuilder: WebClient.Builder, ssl: WebClientSsl) {
private val webClient: WebClient
init {
webClient = webClientBuilder.baseUrl("https://example.org")
.apply(ssl.fromBundle("mybundle")).build()
}
fun someRestCall(name: String?): Mono<Details> {
return webClient.get().uri("/{name}/details", name)
.retrieve().bodyToMono(Details::class.java)
}
}
RestClient
如果你的应用程序没有使用 Spring WebFlux 或 Project Reactor,我们建议你使用 RestClient 来调用远程 REST 服务。
RestClient 接口提供了一种函数式风格的阻塞 API。
Spring Boot 会为你创建并预配置一个原型的 RestClient.Builder Bean。强烈建议在你的组件中注入该 Builder,并使用它来创建 RestClient 实例。Spring Boot 会使用 HttpMessageConverters 和合适的 ClientHttpRequestFactory 来配置该 Builder。
以下代码展示了一个典型示例:
- Java
- Kotlin
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
@Service
public class MyService {
private final RestClient restClient;
public MyService(RestClient.Builder restClientBuilder) {
this.restClient = restClientBuilder.baseUrl("https://example.org").build();
}
public Details someRestCall(String name) {
return this.restClient.get().uri("/{name}/details", name).retrieve().body(Details.class);
}
}
import org.springframework.boot.docs.io.restclient.restclient.ssl.Details
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClient
@Service
class MyService(restClientBuilder: RestClient.Builder) {
private val restClient: RestClient
init {
restClient = restClientBuilder.baseUrl("https://example.org").build()
}
fun someRestCall(name: String?): Details {
return restClient.get().uri("/{name}/details", name)
.retrieve().body(Details::class.java)!!
}
}
RestClient 自定义
有三种主要方法来定制 RestClient,具体取决于你希望定制的范围有多广。
为了使任何自定义的范围尽可能窄,请注入自动配置的 RestClient.Builder,然后按需调用其方法。RestClient.Builder 实例是有状态的:对该构建器的任何修改都会反映在后续使用它创建的所有客户端中。如果你想使用同一个构建器创建多个客户端,也可以考虑通过 RestClient.Builder other = builder.clone(); 来克隆该构建器。
要对所有 RestClient.Builder 实例进行应用范围的、可叠加的自定义,你可以声明 RestClientCustomizer Bean,并在注入点处对 RestClient.Builder 进行局部修改。
最后,你可以回退到原始 API 并使用 RestClient.create()。在这种情况下,不会应用任何自动配置或 RestClientCustomizer。
你也可以更改 全局 HTTP 客户端配置。
RestClient SSL 支持
如果你需要对 RestClient 所使用的 ClientHttpRequestFactory 进行自定义 SSL 配置,可以注入一个 RestClientSsl 实例,并通过构建器的 apply 方法使用它。
RestClientSsl 接口提供了对你在 application.properties 或 application.yaml 文件中定义的任何 SSL bundles 的访问。
以下代码展示了一个典型示例:
- Java
- Kotlin
import org.springframework.boot.autoconfigure.web.client.RestClientSsl;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
@Service
public class MyService {
private final RestClient restClient;
public MyService(RestClient.Builder restClientBuilder, RestClientSsl ssl) {
this.restClient = restClientBuilder.baseUrl("https://example.org").apply(ssl.fromBundle("mybundle")).build();
}
public Details someRestCall(String name) {
return this.restClient.get().uri("/{name}/details", name).retrieve().body(Details.class);
}
}
import org.springframework.boot.autoconfigure.web.client.RestClientSsl
import org.springframework.boot.docs.io.restclient.restclient.ssl.settings.Details
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClient
@Service
class MyService(restClientBuilder: RestClient.Builder, ssl: RestClientSsl) {
private val restClient: RestClient
init {
restClient = restClientBuilder.baseUrl("https://example.org")
.apply(ssl.fromBundle("mybundle")).build()
}
fun someRestCall(name: String?): Details {
return restClient.get().uri("/{name}/details", name)
.retrieve().body(Details::class.java)!!
}
}
如果你需要在 SSL bundle 之外应用其他自定义配置,可以结合使用 ClientHttpRequestFactorySettings 类与 ClientHttpRequestFactoryBuilder:
- Java
- Kotlin
import java.time.Duration;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
@Service
public class MyService {
private final RestClient restClient;
public MyService(RestClient.Builder restClientBuilder, SslBundles sslBundles) {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings
.ofSslBundle(sslBundles.getBundle("mybundle"))
.withReadTimeout(Duration.ofMinutes(2));
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactoryBuilder.detect().build(settings);
this.restClient = restClientBuilder.baseUrl("https://example.org").requestFactory(requestFactory).build();
}
public Details someRestCall(String name) {
return this.restClient.get().uri("/{name}/details", name).retrieve().body(Details.class);
}
}
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.ssl.SslBundles
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClient
import java.time.Duration
@Service
class MyService(restClientBuilder: RestClient.Builder, sslBundles: SslBundles) {
private val restClient: RestClient
init {
val settings = ClientHttpRequestFactorySettings.defaults()
.withReadTimeout(Duration.ofMinutes(2))
.withSslBundle(sslBundles.getBundle("mybundle"))
val requestFactory = ClientHttpRequestFactoryBuilder.detect().build(settings);
restClient = restClientBuilder
.baseUrl("https://example.org")
.requestFactory(requestFactory).build()
}
fun someRestCall(name: String?): Details {
return restClient.get().uri("/{name}/details", name).retrieve().body(Details::class.java)!!
}
}
RestTemplate
Spring Framework 的 RestTemplate 类早于 RestClient,是许多应用程序调用远程 REST 服务的经典方式。当你已有不想迁移到 RestClient 的代码,或者你已经熟悉 RestTemplate API 时,你可能会选择使用 RestTemplate。
由于 RestTemplate 实例通常在使用前需要进行自定义,Spring Boot 并未提供任何单一的自动配置的 RestTemplate Bean。不过,它确实自动配置了一个 RestTemplateBuilder,可在需要时用于创建 RestTemplate 实例。自动配置的 RestTemplateBuilder 能确保为 RestTemplate 实例应用合理的 HttpMessageConverters 和适当的 ClientHttpRequestFactory。
以下代码展示了一个典型示例:
- Java
- Kotlin
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class MyService {
private final RestTemplate restTemplate;
public MyService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public Details someRestCall(String name) {
return this.restTemplate.getForObject("/{name}/details", Details.class, name);
}
}
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
@Service
class MyService(restTemplateBuilder: RestTemplateBuilder) {
private val restTemplate: RestTemplate
init {
restTemplate = restTemplateBuilder.build()
}
fun someRestCall(name: String): Details {
return restTemplate.getForObject("/{name}/details", Details::class.java, name)!!
}
}
RestTemplateBuilder 包含许多有用的方法,可用于快速配置 RestTemplate。例如,要添加 BASIC 认证支持,可以使用 builder.basicAuthentication("user", "password").build()。
RestTemplate 自定义
有三种主要方法来定制 RestTemplate,具体取决于你希望定制的范围有多广。
为了使任何自定义的范围尽可能窄,请注入自动配置的 RestTemplateBuilder,然后根据需要调用其方法。每次方法调用都会返回一个新的 RestTemplateBuilder 实例,因此这些自定义仅影响该构建器的此次使用。
要进行应用范围的、可叠加的自定义,请使用 RestTemplateCustomizer Bean。所有此类 Bean 都会自动注册到自动配置的 RestTemplateBuilder 中,并应用于使用该 Builder 构建的任何模板。
以下示例展示了一个自定义器,它配置了对除 192.168.0.5 之外的所有主机使用代理:
- Java
- Kotlin
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class MyRestTemplateCustomizer implements RestTemplateCustomizer {
@Override
public void customize(RestTemplate restTemplate) {
HttpRoutePlanner routePlanner = new CustomRoutePlanner(new HttpHost("proxy.example.com"));
HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).build();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
static class CustomRoutePlanner extends DefaultProxyRoutePlanner {
CustomRoutePlanner(HttpHost proxy) {
super(proxy);
}
@Override
protected HttpHost determineProxy(HttpHost target, HttpContext context) throws HttpException {
if (target.getHostName().equals("192.168.0.5")) {
return null;
}
return super.determineProxy(target, context);
}
}
}
import org.apache.hc.client5.http.classic.HttpClient
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner
import org.apache.hc.client5.http.routing.HttpRoutePlanner
import org.apache.hc.core5.http.HttpException
import org.apache.hc.core5.http.HttpHost
import org.apache.hc.core5.http.protocol.HttpContext
import org.springframework.boot.web.client.RestTemplateCustomizer
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory
import org.springframework.web.client.RestTemplate
class MyRestTemplateCustomizer : RestTemplateCustomizer {
override fun customize(restTemplate: RestTemplate) {
val routePlanner: HttpRoutePlanner = CustomRoutePlanner(HttpHost("proxy.example.com"))
val httpClient: HttpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).build()
restTemplate.requestFactory = HttpComponentsClientHttpRequestFactory(httpClient)
}
internal class CustomRoutePlanner(proxy: HttpHost?) : DefaultProxyRoutePlanner(proxy) {
@Throws(HttpException::class)
public override fun determineProxy(target: HttpHost, context: HttpContext): HttpHost? {
if (target.hostName == "192.168.0.5") {
return null
}
return super.determineProxy(target, context)
}
}
}
最后,你可以定义自己的 RestTemplateBuilder bean。这样做会替换自动配置的 builder。如果你希望像自动配置那样将任意 RestTemplateCustomizer bean 应用于你的自定义 builder,请使用 RestTemplateBuilderConfigurer 进行配置。以下示例暴露了一个 RestTemplateBuilder,其行为与 Spring Boot 自动配置生成的 builder 一致,但额外指定了自定义的连接超时和读取超时:
- Java
- Kotlin
import java.time.Duration;
import org.springframework.boot.autoconfigure.web.client.RestTemplateBuilderConfigurer;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyRestTemplateBuilderConfiguration {
@Bean
public RestTemplateBuilder restTemplateBuilder(RestTemplateBuilderConfigurer configurer) {
return configurer.configure(new RestTemplateBuilder())
.connectTimeout(Duration.ofSeconds(5))
.readTimeout(Duration.ofSeconds(2));
}
}
import org.springframework.boot.autoconfigure.web.client.RestTemplateBuilderConfigurer
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.time.Duration
@Configuration(proxyBeanMethods = false)
class MyRestTemplateBuilderConfiguration {
@Bean
fun restTemplateBuilder(configurer: RestTemplateBuilderConfigurer): RestTemplateBuilder {
return configurer.configure(RestTemplateBuilder()).connectTimeout(Duration.ofSeconds(5))
.readTimeout(Duration.ofSeconds(2))
}
}
最极端(且很少使用)的选项是创建你自己的 RestTemplateBuilder Bean,而不使用配置器。除了替换自动配置的 builder 之外,这还会阻止任何 RestTemplateCustomizer Bean 被使用。
你也可以更改全局 HTTP 客户端配置。
RestTemplate SSL 支持
如果你需要在 RestTemplate 上进行自定义 SSL 配置,可以将一个 SSL bundle 应用于 RestTemplateBuilder,如下例所示:
- Java
- Kotlin
import org.springframework.boot.docs.io.restclient.resttemplate.Details;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class MyService {
private final RestTemplate restTemplate;
public MyService(RestTemplateBuilder restTemplateBuilder, SslBundles sslBundles) {
this.restTemplate = restTemplateBuilder.sslBundle(sslBundles.getBundle("mybundle")).build();
}
public Details someRestCall(String name) {
return this.restTemplate.getForObject("/{name}/details", Details.class, name);
}
}
import org.springframework.boot.docs.io.restclient.resttemplate.Details
import org.springframework.boot.ssl.SslBundles
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
@Service
class MyService(restTemplateBuilder: RestTemplateBuilder, sslBundles: SslBundles) {
private val restTemplate: RestTemplate
init {
restTemplate = restTemplateBuilder.sslBundle(sslBundles.getBundle("mybundle")).build()
}
fun someRestCall(name: String): Details {
return restTemplate.getForObject("/{name}/details", Details::class.java, name)!!
}
}
RestClient 和 RestTemplate 的 HTTP 客户端检测
Spring Boot 将根据应用程序类路径上可用的库,自动检测与 RestClient 和 RestTemplate 一起使用的 HTTP 客户端。按优先级顺序,支持以下客户端:
-
Apache HttpClient
-
Jetty HttpClient
-
Reactor Netty HttpClient
-
JDK client (
java.net.http.HttpClient) -
Simple JDK client (
java.net.HttpURLConnection)
如果 classpath 中存在多个客户端,且未提供全局配置,则将使用优先级最高的客户端。
全局 HTTP 客户端配置
如果自动检测到的 HTTP 客户端无法满足你的需求,你可以使用 spring.http.client.factory 属性来选择一个特定的工厂。例如,如果你的 classpath 中有 Apache HttpClient,但你更倾向于使用 Jetty 的 HttpClient,你可以添加以下内容:
- Properties
- YAML
spring.http.client.factory=jetty
spring:
http:
client:
factory: jetty
你也可以设置属性来更改将应用于所有客户端的默认值。例如,你可能想要更改超时时间以及是否跟随重定向:
- Properties
- YAML
spring.http.client.connect-timeout=2s
spring.http.client.read-timeout=1s
spring.http.client.redirects=dont-follow
spring:
http:
client:
connect-timeout: 2s
read-timeout: 1s
redirects: dont-follow
对于更复杂的自定义需求,你可以使用 ClientHttpRequestFactoryBuilderCustomizer,或者声明你自己的 ClientHttpRequestFactoryBuilder Bean,这将导致自动配置退避。当你需要自定义底层 HTTP 库的一些内部细节时,这种方式会很有用。
例如,以下代码将使用一个配置了特定 ProxySelector 的 JDK 客户端:
- Java
- Kotlin
import java.net.ProxySelector;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyClientHttpConfiguration {
@Bean
ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder(ProxySelector proxySelector) {
return ClientHttpRequestFactoryBuilder.jdk()
.withHttpClientCustomizer((builder) -> builder.proxy(proxySelector));
}
}
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.net.ProxySelector
@Configuration(proxyBeanMethods = false)
class MyClientHttpConfiguration {
@Bean
fun clientHttpRequestFactoryBuilder(proxySelector: ProxySelector): ClientHttpRequestFactoryBuilder<*> {
return ClientHttpRequestFactoryBuilder.jdk()
.withHttpClientCustomizer { builder -> builder.proxy(proxySelector) }
}
}