To integrate Spring Boot with an external API, use RestClient for synchronous calls (Spring Framework 6.1 and Spring Boot 3.2 or newer) or WebClient when you need non-blocking, reactive calls. RestTemplate still works but is in maintenance mode, so new code should not start with it. Whichever client you pick, configure the base URL, credentials and timeouts in one place, map responses to records, and translate HTTP errors into your own exceptions.
This guide builds one small weather client three ways: RestClient, WebClient, and a declarative @HttpExchange interface. It also covers timeouts, error handling, retries and testing.
| Client | Style | Starter | Use it when |
|---|---|---|---|
RestClient | Synchronous, fluent | spring-boot-starter-web | Default choice for servlet apps, including with virtual threads |
WebClient | Non-blocking, reactive | spring-boot-starter-webflux | You already use Reactor, or you must call many APIs in parallel |
RestTemplate | Synchronous, template methods | spring-boot-starter-web | Maintaining existing code only |
@HttpExchange interface | Declarative proxy | Backed by RestClient or WebClient | You want a typed client without boilerplate |
RestClient has the same fluent API shape as WebClient but blocks the calling thread, which is simple to reason about and works well with Java 21 virtual threads. Spring Boot auto-configures a RestClient.Builder bean, so you do not construct clients from scratch.
Create one client per external service. Keep the base URL and API key in configuration, never in code.
The spring.http.client.* timeouts (Spring Boot 3.4 and newer) apply to clients built from the auto-configured builder.
Model the response as a record. Jackson maps JSON fields to record components by name.
retrieve() sends the request and body(...) deserialises the response. Use uri with template variables, as above, so values are URL-encoded for you instead of concatenated into the string.
By default retrieve() throws HttpClientErrorException for 4xx and HttpServerErrorException for 5xx. Translate them into exceptions that mean something in your domain.
Network failures (connection refused, timeouts) surface as ResourceAccessException. Catch it at the boundary where you can decide to retry, fall back or fail the request. Use toEntity(Forecast.class) instead of body(...) when you also need headers or the status code.
Add spring-boot-starter-webflux and build the client from the auto-configured WebClient.Builder.
Nothing happens until something subscribes to the returned Mono, which a WebFlux controller does for you. Avoid calling block() inside a reactive pipeline, because it defeats the point of the non-blocking client and can fail on event-loop threads. To call several APIs at once, combine the results with Mono.zip(...).
An HTTP interface turns the API into a typed Java interface. Spring generates the implementation on top of RestClient.
Inject WeatherApi wherever you need it. Newer Spring versions can also register such interfaces declaratively with @ImportHttpServices, so check the reference documentation for your Spring Boot version.
An external API will eventually be slow or down, so plan for it.
@Retryable; Resilience4j is the common alternative and also offers circuit breakers and rate limiters.Test the client without the real service. For RestClient, bind a MockRestServiceServer to the builder.
For more realistic tests (timeouts, malformed bodies, slow responses), run WireMock and point weather.base-url at it.
It is in maintenance mode and Spring recommends RestClient for new code. Existing RestTemplate code keeps working, and you can migrate gradually because RestClient can reuse the same message converters.
Only if you need its non-blocking features. In a servlet application RestClient is simpler, and with virtual threads blocking calls scale well.
In environment variables, a secrets manager or Spring Cloud Config, injected through @Value or @ConfigurationProperties. Do not commit them to application.yml.
Use Spring Security's OAuth2 client support and add its request interceptor or exchange filter to your client, so tokens are obtained and refreshed automatically.
Use RestClient for blocking calls and WebClient for reactive ones, build each from the auto-configured builder, and keep the base URL, key and timeouts in configuration. Map responses to records, convert HTTP errors into domain exceptions, retry only safe requests, and test the client against a mock or WireMock server. Consider an @HttpExchange interface when you want a typed, boilerplate-free client.