Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you integrate Spring Boot with external APIs?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJan 24, 2025
UpdatedNov 16, 2026
7 min read

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.

Table of Contents

  • Choosing between RestClient, WebClient and RestTemplate
  • Calling an external API with RestClient
  • Handling errors from the remote API
  • Non-blocking calls with WebClient
  • Declaring the client as an interface
  • Timeouts, retries and resilience
  • Testing the integration
  • Frequently Asked Questions
    • Is RestTemplate deprecated?
    • Should I use WebClient in a non-reactive application?
    • Where should I keep API keys?
    • How do I call an API that needs OAuth 2.0 tokens?
  • Summary

Choosing between RestClient, WebClient and RestTemplate

ClientStyleStarterUse it when
RestClientSynchronous, fluentspring-boot-starter-webDefault choice for servlet apps, including with virtual threads
WebClientNon-blocking, reactivespring-boot-starter-webfluxYou already use Reactor, or you must call many APIs in parallel
RestTemplateSynchronous, template methodsspring-boot-starter-webMaintaining existing code only
@HttpExchange interfaceDeclarative proxyBacked by RestClient or WebClientYou 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.

Calling an external API with RestClient

Create one client per external service. Keep the base URL and API key in configuration, never in code.

yaml

The spring.http.client.* timeouts (Spring Boot 3.4 and newer) apply to clients built from the auto-configured builder.

java

Model the response as a record. Jackson maps JSON fields to record components by name.

java
java

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.

Handling errors from the remote API

By default retrieve() throws HttpClientErrorException for 4xx and HttpServerErrorException for 5xx. Translate them into exceptions that mean something in your domain.

java

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.

Non-blocking calls with WebClient

Add spring-boot-starter-webflux and build the client from the auto-configured WebClient.Builder.

java

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(...).

Declaring the client as an interface

An HTTP interface turns the API into a typed Java interface. Spring generates the implementation on top of RestClient.

java
java

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.

Timeouts, retries and resilience

An external API will eventually be slow or down, so plan for it.

  1. Always set connect and read timeouts. A call without a timeout can hold a thread indefinitely.
  2. Retry only idempotent requests (GET, PUT, DELETE), with a small attempt count and backoff. Spring Framework 7 adds @Retryable; Resilience4j is the common alternative and also offers circuit breakers and rate limiters.
  3. Return a fallback or a clear 503 from your own API when the dependency is unavailable instead of letting the exception bubble up as a 500.
  4. Never log API keys or full request headers.

Testing the integration

Test the client without the real service. For RestClient, bind a MockRestServiceServer to the builder.

java

For more realistic tests (timeouts, malformed bodies, slow responses), run WireMock and point weather.base-url at it.

Frequently Asked Questions

Is RestTemplate deprecated?

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.

Should I use WebClient in a non-reactive application?

Only if you need its non-blocking features. In a servlet application RestClient is simpler, and with virtual threads blocking calls scale well.

Where should I keep API keys?

In environment variables, a secrets manager or Spring Cloud Config, injected through @Value or @ConfigurationProperties. Do not commit them to application.yml.

How do I call an API that needs OAuth 2.0 tokens?

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.

Summary

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.

Share