跳到主要内容
版本:4.0.2

调用 REST 服务

QWen Max 中英对照 Calling REST Services

Spring Boot 提供了多种便捷的方式来调用远程 REST 服务。如果你正在开发一个非阻塞的响应式应用程序,并且使用的是 Spring WebFlux,那么你可以使用 WebClient。如果你更喜欢命令式(imperative)API,那么可以使用 RestClientRestTemplate

WebClient

如果你的 classpath 中包含 Spring WebFlux,我们建议你使用 WebClient 来调用远程 REST 服务。WebClient 接口提供了函数式风格的 API,并且是完全响应式的。你可以在 Spring Framework 文档的专门 章节 中了解更多关于 WebClient 的内容。

提示

如果你没有编写响应式 Spring WebFlux 应用程序,可以使用 RestClient 而不是 WebClient。它提供了类似的函数式 API,但是命令式的而非响应式的。

Spring Boot 会为你创建并预配置一个原型(prototype)的 WebClient.Builder bean。强烈建议你在组件中注入该 builder,并用它来创建 WebClient 实例。Spring Boot 会对该 builder 进行配置,使其共享 HTTP 资源,并以与服务器端相同的方式反映编解码器(codecs)的设置(参见 WebFlux HTTP 编解码器自动配置),以及其他功能。

以下代码展示了一个典型示例:

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);
}

}

WebClient Runtime

Spring Boot 会根据应用程序 classpath 中可用的库,自动检测使用哪个 ClientHttpConnector 来驱动 WebClient。按优先级顺序,支持以下客户端:

  1. Reactor Netty

  2. Jetty RS client

  3. Apache HttpClient

  4. JDK HttpClient

如果 classpath 上有多个客户端可用,则将使用优先级最高的客户端。

spring-boot-starter-webflux starter 默认依赖于 io.projectreactor.netty:reactor-netty,它同时提供了服务器和客户端的实现。如果你选择使用 Jetty 作为响应式服务器,则应添加 Jetty 响应式 HTTP 客户端库 org.eclipse.jetty:jetty-reactive-httpclient 的依赖。对服务器和客户端使用相同的技术具有一定的优势,因为它会自动在客户端和服务器之间共享 HTTP 资源。

开发者可以通过提供自定义的 ReactorResourceFactoryJettyResourceFactory Bean 来覆盖 Jetty 和 Reactor Netty 的资源配置——该配置将同时应用于客户端和服务器。

如果你想为客户端覆盖该选择,可以定义自己的 ClientHttpConnector Bean,从而完全控制客户端的配置。

你可以在 Spring Framework 参考文档 中了解更多关于 WebClient 配置选项的信息。

全局 HTTP 连接器配置

如果自动检测到的 ClientHttpConnector 无法满足你的需求,你可以使用 spring.http.clients.reactive.connector 属性来指定一个特定的连接器。例如,如果你的 classpath 中包含 Reactor Netty,但你更倾向于使用 Jetty 的 HttpClient,你可以添加以下配置:

spring.http.clients.reactive.connector=jetty
提示

你也可以使用 全局配置属性,这些属性适用于所有 HTTP 客户端。

对于更复杂的自定义需求,你可以使用 ClientHttpConnectorBuilderCustomizer,或者声明你自己的 ClientHttpConnectorBuilder Bean,这将导致自动配置退避。当你需要自定义底层 HTTP 库的一些内部细节时,这种方式会很有用。

例如,以下代码将使用一个配置了特定 ProxySelector 的 JDK 客户端:

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));
}

}

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.propertiesapplication.yaml 文件中定义的任何 SSL bundles 的访问。

以下代码展示了一个典型示例:

import reactor.core.publisher.Mono;

import org.springframework.boot.webclient.autoconfigure.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);
}

}

RestClient

如果你的应用程序未使用 Spring WebFlux 或 Project Reactor,我们建议你使用 RestClient 来调用远程 REST 服务。

RestClient 接口提供了一种函数式风格的命令式 API。

Spring Boot 会为你创建并预配置一个原型的 RestClient.Builder Bean。强烈建议在你的组件中注入该 Builder,并使用它来创建 RestClient 实例。Spring Boot 会为该 Builder 配置 HttpMessageConverters 以及合适的 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) {
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);
}

}

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.propertiesapplication.yaml 文件中定义的任何 SSL bundles 的访问。

以下代码展示了一个典型示例:

import org.springframework.boot.restclient.autoconfigure.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);
}

}

如果你需要在 SSL bundle 之外应用其他自定义配置,可以结合使用 HttpClientSettings 类与 ClientHttpRequestFactoryBuilder

import java.time.Duration;

import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.HttpClientSettings;
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) {
HttpClientSettings settings = HttpClientSettings.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);
}

}

RestTemplate

Spring Framework 的 RestTemplate 类早于 RestClient,是许多应用程序调用远程 REST 服务的经典方式。当你已有不想迁移到 RestClient 的代码,或者你已经熟悉 RestTemplate API 时,你可能会选择使用 RestTemplate

由于 RestTemplate 实例通常需要在使用前进行自定义,Spring Boot 不会提供任何单一的自动配置的 RestTemplate Bean。不过,它会自动配置一个 RestTemplateBuilder,可在需要时用于创建 RestTemplate 实例。自动配置的 RestTemplateBuilder 会确保为 RestTemplate 实例应用合理的 HttpMessageConverters 和适当的 ClientHttpRequestFactory

以下代码展示了一个典型示例:

import org.springframework.boot.restclient.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);
}

}

RestTemplateBuilder 包含许多实用方法,可用于快速配置 RestTemplate。例如,要添加 BASIC 认证支持,可以使用 builder.basicAuthentication("user", "password").build()

RestTemplate 自定义

有三种主要方法来定制 RestTemplate,具体取决于你希望定制的范围有多广。

为了使任何自定义的范围尽可能窄,请注入自动配置的 RestTemplateBuilder,然后根据需要调用其方法。每次方法调用都会返回一个新的 RestTemplateBuilder 实例,因此这些自定义仅影响该构建器的此次使用。

要进行应用范围的、可叠加的自定义配置,请使用 RestTemplateCustomizer Bean。所有此类 Bean 都会自动注册到自动配置的 RestTemplateBuilder 中,并应用于通过它构建的所有模板。

以下示例展示了一个自定义器,它配置了对除 192.168.0.5 之外的所有主机使用代理:

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.restclient.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);
}

}

}

最后,你可以定义自己的 RestTemplateBuilder bean。这样做会替换自动配置的 builder。如果你希望像自动配置那样将任意 RestTemplateCustomizer bean 应用于你的自定义 builder,请使用 RestTemplateBuilderConfigurer 进行配置。以下示例暴露了一个 RestTemplateBuilder,其行为与 Spring Boot 自动配置的效果一致,只是额外指定了自定义的连接超时和读取超时:

import java.time.Duration;

import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.boot.restclient.autoconfigure.RestTemplateBuilderConfigurer;
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));
}

}

最极端(且很少使用)的选项是创建你自己的 RestTemplateBuilder Bean,而不使用配置器。这样做不仅会替换掉自动配置的 builder,还会阻止任何 RestTemplateCustomizer Bean 被使用。

提示

你也可以更改 全局 HTTP 客户端配置

RestTemplate SSL 支持

如果你需要在 RestTemplate 上进行自定义 SSL 配置,可以将一个 SSL bundle 应用于 RestTemplateBuilder,如下例所示:

import org.springframework.boot.docs.io.restclient.resttemplate.Details;
import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.boot.ssl.SslBundles;
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);
}

}

RestClient 和 RestTemplate 的 HTTP 客户端检测

Spring Boot 将根据应用程序类路径上可用的库,自动检测与 RestClientRestTemplate 一起使用的 HTTP 客户端。按优先级顺序,支持以下客户端:

  1. Apache HttpClient

  2. Jetty HttpClient

  3. Reactor Netty HttpClient

  4. JDK client(java.net.http.HttpClient

  5. Simple JDK client(java.net.HttpURLConnection

如果 classpath 中存在多个客户端,且未提供全局配置,则将使用优先级最高的客户端。

全局 HTTP 客户端配置

如果自动检测到的 HTTP 客户端无法满足你的需求,你可以使用 spring.http.clients.imperative.factory 属性来指定一个特定的工厂。例如,如果你的 classpath 中有 Apache HttpClient,但你更倾向于使用 Jetty 的 HttpClient,你可以添加以下内容:

spring.http.clients.imperative.factory=jetty
提示

你也可以使用 全局配置属性,这些属性适用于所有 HTTP 客户端。

对于更复杂的自定义需求,你可以使用 ClientHttpRequestFactoryBuilderCustomizer,或者声明你自己的 ClientHttpRequestFactoryBuilder bean,这将导致自动配置退让(back off)。当你需要自定义底层 HTTP 库的一些内部细节时,这种方式会很有用。

例如,以下代码将使用一个配置了特定 ProxySelector 的 JDK 客户端:

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));
}

}

API 版本控制

WebClientRestClient 均支持发起带版本的远程 HTTP 调用,以便 API 能够随时间演进。通常,这涉及发送一个 HTTP 头、查询参数或 URL 路径段,以指明应使用的 API 版本。

你可以使用 WebClient.BuilderRestClient.Builder 上的方法来配置 API 版本控制。

提示

服务端也支持 API 版本控制。详见 Spring MVCSpring WebFlux 章节。

备注

服务器端的 API 版本控制配置不会被用于自动配置客户端。如果客户端需要使用 API 版本控制策略(通常用于测试),则必须显式地进行配置。

HTTP Service Interface Clients

除了直接使用 RestClientWebClient 来调用 HTTP 服务之外,还可以通过带注解的 Java 接口来调用它们。

HTTP Service 接口通过使用带有 @HttpExchange 注解的方法来定义服务契约,或者更常见的是使用特定于方法的变体(@GetExchange@PostExchange@DeleteExchange 等)。

例如,以下代码定义了一个用于 “echo” API 的 HTTP 服务,该服务将返回一个包含请求回显的 JSON 对象。

import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PostExchange;

@HttpExchange(url = "https://echo.zuplo.io")
public interface EchoService {

@PostExchange
Map<?, ?> echo(@RequestBody Map<String, String> message);

}

有关如何开发 HTTP Service 接口客户端的更多详细信息,请参阅 Spring Framework 参考文档

导入 HTTP 服务

为了将 HTTP Service 接口用作客户端,你需要导入它。一种实现方式是使用 @ImportHttpServices 注解,通常将其添加到你的主应用程序类上。你可以使用该注解导入特定的类,或者从指定的包中扫描需要导入的类。

例如,以下配置将在 com.example.myclients 包中扫描 HTTP Service 接口:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.service.registry.ImportHttpServices;

@SpringBootApplication
@ImportHttpServices(basePackages = "com.example.myclients")
public class MyApplication {

public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}

}

Service Client Groups

在生产应用中,硬编码绝对 URL 到 @HttpExchange 注解通常并不理想。相反,你通常希望在代码中为 HTTP 服务客户端指定一个逻辑名称,然后根据该名称从属性中查找对应的 URL。

HTTP Service 客户端允许你通过将它们注册到命名组中来实现这一点。一个 HTTP Service 组是一组共享共同特性的 HTTP Service 接口的集合。

例如,我们可能想要定义一个名为 “echo” 的 group,用于调用 https://echo.zuplo.io 的 HTTP Service 客户端。

备注

HTTP 服务组不仅可以用于定义 URL。例如,你的组可以定义连接超时和 SSL 设置。你还可以将客户端自定义逻辑关联到一个组,例如添加代码以插入所需的授权头。

在使用 @ImportHttpServices 时,若要将 HTTP Service 接口与一个组关联,可以使用 group 属性。

例如,假设我们上面的示例是以这样的方式组织的:com.example.myclients 包中的所有 HTTP Service 接口都属于 echo 组。我们首先从服务接口中移除硬编码的 URL:

import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.PostExchange;

public interface EchoService {

@PostExchange
Map<?, ?> echo(@RequestBody Map<String, String> message);

}

我们就可以写成:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.service.registry.ImportHttpServices;

@SpringBootApplication
@ImportHttpServices(group = "echo", basePackages = "com.example.myclients")
public class MyApplication {

public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}

}

最后,我们可以使用 base-url 属性将 echo 组链接到一个实际的 URL:

spring.http.serviceclient.echo.base-url=https://echo.zuplo.io
提示

如果你不指定 group,HTTP Service 客户端将被关联到名为 “default” 的 group。

备注

如果同一个包中有多个 HTTP Service 接口需要关联到不同的组,你可以逐个列出它们。@ImportHttpServices 注解是可重复的,其 types 属性允许你导入单个类。

例如:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.service.registry.ImportHttpServices;

@SpringBootApplication
@ImportHttpServices(group = "echo", types = EchoService.class)
@ImportHttpServices(group = "other", types = OtherService.class)
public class MyApplication {

public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}

}

配置属性

HTTP 服务的配置属性可以在 spring.http.serviceclient.<group-name> 下指定:

你可以使用属性来配置以下方面的内容:

  • 基础 URL。

  • 应发送的任何默认请求头。

  • API 版本控制配置。

  • 重定向设置。

  • 连接和读取超时设置。

  • 要使用的 SSL 证书包。

提示

你也可以使用 全局配置属性,这些属性适用于所有 HTTP 客户端。

例如,下面的属性将:

  • 配置所有 HTTP 客户端使用 1 秒的连接超时(除非另有覆盖)。

  • 配置 “echo” 组中的 HTTP 服务客户端:

    • 使用特定的 base URL。

    • 设置 2 秒的读取超时。

    • 使用 X-Version 头插入 API 版本信息。

spring.http.clients.connect-timeout=1s
spring.http.serviceclient.echo.base-url=https://echo.zuplo.io
spring.http.serviceclient.echo.read-timeout=2s;
spring.http.serviceclient.echo.apiversion.default=1.0.0
spring.http.serviceclient.echo.apiversion.insert.header=X-Version

自定义

如果需要对 HTTP Service 客户端进行超出基本属性的自定义,可以使用 HTTP Service 组配置器。对于基于 RestClient 的 HTTP Service 客户端,可以声明一个实现 RestClientHttpServiceGroupConfigurer 的 Bean。对于基于 WebClient 的 HTTP Service 客户端,可以声明一个实现 WebClientHttpServiceGroupConfigurer 的 Bean。

两者的工作方式相同,并将由 Spring Boot 的自动配置自动应用。

例如,以下配置将添加一个组自定义器(group customizer),它会在每个传出请求中添加一个包含组名称的 HTTP 头:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.support.RestClientHttpServiceGroupConfigurer;

@Configuration(proxyBeanMethods = false)
public class MyHttpServiceGroupConfiguration {

@Bean
RestClientHttpServiceGroupConfigurer myHttpServiceGroupConfigurer() {
return (groups) -> groups.forEachClient((group, clientBuilder) -> {
String groupName = group.name();
clientBuilder.defaultHeader("service-group", groupName);
});
}

}

高级配置

除了 @ImportHttpServices 注解外,Spring Framework 还提供了一个 AbstractHttpServiceRegistrar 类。你可以 @Import 该类的自定义扩展,以执行编程式配置。更多详情,请参阅 Spring Framework 参考文档

无论你使用哪种方法注册 HTTP Service 客户端,Spring Boot 的支持都保持不变。

将全局配置应用于所有 HTTP 客户端

无论使用何种底层技术,所有 HTTP 客户端都具有可配置的通用设置。

这些包括:

  • 连接超时。

  • 读取超时。

  • HTTP 重定向应如何处理。

  • 连接时应使用哪个 SSL 证书包。

这些通用设置由 HttpClientSettings 类表示,该类可以传入 ClientHttpConnectorBuilderClientHttpRequestFactoryBuilderbuild(…​) 方法中。

如果你想将相同的配置应用于所有自动配置的客户端,可以使用 spring.http.clients 属性来实现:

spring.http.clients.connect-timeout=2s
spring.http.clients.read-timeout=1s
spring.http.clients.redirects=dont-follow