Declarative REST client

Spring Framework 6

Posted by Xavier Bouclet on February 04, 2024 · 4 mins read

Declarative REST client

1. Purpose of this blog post

To create a REST client easily with Spring, we often use feign. For Spring Boot, we can use :

  • for webmvc

  • com.playtika.reactivefeign:feign-reactor-spring-cloud-starter for webflux

There is no unified way of creating a feign client, at least prior to Spring Framework 6.

Now, with Spring Framework 6 (or Spring Boot 3), we can leverage the declarative REST client.

TODO ⇒ code

2. Creating a declarative REST client

Let’s use the Spring Initialzr to create the Spring Boot project.

  • Group : com.xavierbouclet

  • Artifact : whiskies-api

  • Project : Maven

  • Language : Java

  • Java : 17

  • Dependencies :

    • Spring Web

    • Spring Reactive Web

    • Validation

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->


Let’s create an interface for our REST client.

public interface WhiskyService {

    List<Whisky> loadAll();
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;

@ConfigurationProperties(prefix = "client.whisky.service")
public record WhiskyClientProperties(@NotNull String url) {
    WebClient webClient(WhiskyClientProperties whiskyClientProperties) {
        return  WebClient.builder().baseUrl(whiskyClientProperties.url())
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

    HttpServiceProxyFactory proxyFactory(WebClient client) {
        return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();

    WhiskyService whiskyService(HttpServiceProxyFactory factory) {
        return factory.createClient(WhiskyService.class);

3. Adapt the client for Webflux

public interface WhiskyService {

    Flux<Whisky> loadAll();

4. Conclusion

In my point of view, Declarative REST client is a nicer way to declare REST client than Feign.

If you want to check the final code on GitHub.

Follow Me