Getting Started with Spring 6 HTTP Interface: A Complete Demo
This article introduces Spring Framework 6's new HTTP Interface feature, walks through creating a simple Spring Boot service, defining a Java interface for HTTP calls, testing it with WebClient and HttpServiceProxyFactory, and explains related annotations and dependencies.
Spring Framework 6's first GA release includes the HTTP Interface feature, allowing developers to declare HTTP services as Java interfaces annotated with specific markers and invoke them as regular method calls.
Demo Overview
First, a simple Spring Boot project is created (minimum version 3.0.0, requiring Java 17). An entity class User and a controller exposing /users that returns a list of ten users are defined.
public class User implements Serializable {
private int id;
private String name;
// constructors, getters, setters omitted
@Override
public String toString() {
return id + ":" + name;
}
} @GetMapping("/users")
public List
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 the user list.
Next, the HTTP Interface is defined:
public interface UserApiService {
@GetExchange("/users")
List
getUsers();
}A test method uses WebClient and HttpServiceProxyFactory to create a proxy instance of UserApiService and invoke getUsers() :
@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
users = service.getUsers();
for (User user : users) {
System.out.println(user);
}
}The output prints each user (e.g., 1:User1 … 10:User10 ).
GetExchange (HttpExchange) Annotation
The @GetExchange annotation marks a method to perform an HTTP GET request; similar annotations exist for other HTTP methods and are defined in the spring-web module under org.springframework.web.service.annotation . The source of HttpExchange shows its attributes such as value , url , method , etc.
@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 Proxy
The proxy is built via HttpServiceProxyFactory , similar to Spring AOP proxy creation. Future Spring versions may provide a more convenient annotation for proxy generation.
Why Spring Reactive Web Dependency?
The demo uses WebClient from Spring Reactive Web because the current HTTP Interface implementation relies on it; a RestTemplate‑based implementation is planned for later releases.
Summary
The article demonstrates the basic usage of Spring 6's HTTP Interface, covering entity and controller setup, interface definition with @GetExchange , proxy creation, testing, and notes on related annotations and required dependencies.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.