Master Spring 6 HTTP Interface: Build a Reactive API Client in Minutes

This guide walks you through creating a Spring Boot 3 project, defining a Java interface annotated with Spring 6's new HTTP Interface, and using WebClient and HttpServiceProxyFactory to call a REST endpoint, while covering required dependencies, annotations, and proxy creation details.

Programmer DD
Programmer DD
Programmer DD
Master Spring 6 HTTP Interface: Build a Reactive API Client in Minutes

Spring 6 HTTP Interface Overview

Spring 6’s first GA release introduces the HTTP Interface feature, which lets developers define HTTP services as Java interfaces annotated with specific Spring annotations. Calling a method on such an interface triggers an HTTP request, similar to Feign.

Demo Setup

Create a simple Spring Boot 3 project (minimum version 3.0.0) using Java 17 or higher, and include spring-web and spring-webflux dependencies.

Define an entity class:

public class User implements Serializable {
    private int id;
    private String name;
    // constructors, getters, setters omitted
    @Override
    public String toString() {
        return id + ":" + name;
    }
}

Define a controller that returns a list of users:

@GetMapping("/users")
public List<User> list() {
    return IntStream.rangeClosed(1, 10)
        .mapToObj(i -> new User(i, "User" + i))
        .collect(Collectors.toList());
}

After starting the service, http://localhost:8080/users returns ten user objects.

Defining the HTTP Interface

Create an interface that describes the remote call:

public interface UserApiService {
    @GetExchange("/users")
    List<User> getUsers();
}

Testing the Interface

Use WebClient and HttpServiceProxyFactory to create a client instance and invoke the method:

@Test
void getUsers() {
    WebClient client = WebClient.builder().baseUrl("http://localhost:8080/").build();
    HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
    UserApiService service = factory.createClient(UserApiService.class);
    List<User> users = service.getUsers();
    for (User user : users) {
        System.out.println(user);
    }
}

The console prints:

1:User1
2:User2
...
9:User9
10:User10

GetExchange (HttpExchange) Annotation

The @GetExchange annotation maps a method to an HTTP GET request. It belongs to the org.springframework.web.service.annotation package in the spring-web module. Other annotations (e.g., @PostExchange, @PutExchange) are specializations of @HttpExchange, analogous to Spring MVC’s @RequestMapping family.

Source of @HttpExchange:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
@Reflective(HttpExchangeReflectiveProcessor.class)
public @interface HttpExchange {
    @AliasFor("url")
    String value() default "";
    @AliasFor("value")
    String url() default "";
    String method() default "";
    String contentType() default "";
    String[] accept() default {};
}

Creating the UserApiService Instance

The client is created via HttpServiceProxyFactory, which returns a proxy object. This proxy can be defined as a @Bean for injection into other components.

Other Features

Methods annotated with @HttpExchange can accept various parameter types and return any custom entity, similar to Spring MVC controllers. Custom exception handling is also supported.

Why Spring Reactive Web Dependency?

Currently, HTTP Interface is implemented only with WebClient from Spring Reactive Web. Future releases may add a RestTemplate‑based implementation.

Spring 6 HTTP Interface demo
Spring 6 HTTP Interface demo
Spring Boot project setup
Spring Boot project setup
HttpExchange annotations
HttpExchange annotations
Proxy object in debug
Proxy object in debug
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootreactivewebclientHTTP Interface
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.