← All articles
Backend1 min read

Calling REST APIs with Spring's RestClient

Spring's RestClient offers a modern, fluent, synchronous HTTP client to replace RestTemplate. Here's how to call external services cleanly.

S

Swapnika Voora

Author

Most services need to call other services. RestClient, introduced in Spring Framework 6.1, gives you a fluent synchronous API that replaces the aging RestTemplate without forcing you into reactive types.

Build a configured client

Create the client as a bean with a base URL and default headers so callers stay concise.

ClientConfig.java
@Configuration
public class ClientConfig {
 
    @Bean
    public RestClient paymentsClient(RestClient.Builder builder) {
        return builder
            .baseUrl("https://payments.internal")
            .defaultHeader("Accept", "application/json")
            .build();
    }
}

Make requests fluently

The fluent chain reads like the HTTP call itself. body(Class) deserializes the response for you.

PaymentGateway.java
@Service
public class PaymentGateway {
 
    private final RestClient client;
 
    public Receipt charge(ChargeRequest request) {
        return client.post()
            .uri("/charges")
            .body(request)
            .retrieve()
            .body(Receipt.class);
    }
}

Handle errors explicitly

Use onStatus to convert HTTP error codes into domain exceptions instead of leaking raw status codes upward.

errors.java
Receipt receipt = client.post()
    .uri("/charges")
    .body(request)
    .retrieve()
    .onStatus(HttpStatusCode::is4xxClientError, (req, res) -> {
        throw new PaymentRejectedException(res.getStatusCode());
    })
    .body(Receipt.class);

Takeaways

  • Prefer RestClient over RestTemplate for new synchronous code.
  • Configure a base URL and defaults once as a bean.
  • Translate HTTP errors into domain exceptions with onStatus.

Set connect and read timeouts on the underlying request factory — a hung dependency should never hang your service indefinitely.

#java#spring-boot#rest#http-client

More in Backend