Backend Development 7 min read

Spring 6 HTTP Interface Demo: Defining and Invoking HTTP Services with Java Interfaces

This article demonstrates how to use Spring Framework 6's new HTTP Interface feature by creating a simple Spring Boot application, defining a User entity and controller, implementing an HTTP Interface with @GetExchange, and testing the service via a WebClient proxy, while covering required dependencies and key annotations.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Spring 6 HTTP Interface Demo: Defining and Invoking HTTP Services with Java Interfaces

Spring Framework 6 introduced the HTTP Interface feature, allowing developers to define HTTP services as annotated Java interfaces and invoke them like regular method calls.

The article walks through a complete demo: a simple User entity, a controller exposing http://localhost:8080/users , and a Spring Boot project (minimum version 3.0.0, Java 17) that includes Spring Web and Spring Reactive Web dependencies.

public class User implements Serializable {
    private int id;
    private String name;
    //省略构造方法、Getter和Setter
    @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, the endpoint returns a list of ten users.

Next, an HTTP Interface is defined:

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

A test method creates a WebClient, builds an HttpServiceProxyFactory, obtains a UserApiService proxy, calls getUsers() , and prints each user.

@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 shows users 1 to 10:

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

The article explains the GetExchange (HttpExchange) annotation, its placement in the spring-web module, and shows the source of the HttpExchange annotation.

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

It also discusses how the proxy instance is created, the current reliance on Spring Reactive Web's WebClient , and notes that future versions may add RestTemplate support.

Additional features such as parameter handling, custom return types, and exception handling are briefly mentioned.

In summary, the guide provides a basic understanding of Spring 6's HTTP Interface and encourages further exploration.

Javabackend-developmentSpringSpring BootWebClientHttp Interface
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.