Backend Development 7 min read

Creating a Simple Demo with Spring 6 HTTP Interface

This article introduces Spring 6's new HTTP Interface feature, walks through building a Spring Boot demo with a User entity, a controller, an HTTP Interface annotated with @GetExchange, and a test using WebClient and HttpServiceProxyFactory, and explains related annotations and reactive dependencies.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Creating a Simple Demo with Spring 6 HTTP Interface

Spring 6's first GA release adds the HTTP Interface feature, allowing developers to define HTTP services as Java interfaces with specific annotations. This tutorial demonstrates a complete demo using Spring Boot 3.x, Java 17, and Spring Reactive Web.

Step 1: Create a simple Spring Boot project (minimum version 3.0.0) and add dependencies for spring-web and spring-webflux .

Step 2: Define a User entity :

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

Step 3: Add a controller to expose 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 application, http://localhost:8080/users returns a list of ten users.

Step 4: Define the HTTP Interface :

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

Step 5: Write a test method that creates a WebClient , builds an HttpServiceProxyFactory , and obtains a proxy instance of UserApiService :

@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 test prints the ten user entries.

GetExchange (HttpExchange) annotation is a specialization of @HttpExchange that maps a method to an HTTP GET request. Other similar annotations exist for different HTTP verbs.

The HttpExchange annotation is defined in the spring-web module under org.springframework.web.service.annotation . Its source includes attributes such as value , url , method , contentType , and accept .

@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 {};
}

The proxy created by HttpServiceProxyFactory is an AOP‑style dynamic proxy; future Spring versions may provide more convenient creation methods.

Other features include support for various method parameters, custom return types, and exception handling, similar to Spring MVC controllers.

Why Spring Reactive Web is required : The current HTTP Interface implementation relies on WebClient from Spring Reactive Web. A RestTemplate‑based implementation is planned for later releases.

Summary : The article gives a quick overview of Spring 6's HTTP Interface, demonstrates a minimal working example, and points out areas for further exploration.

backendJavaSpringReactiveWebClientHttp Interface
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login 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.