Mastering Spring Boot 3’s @HttpExchange for Declarative HTTP Calls

This tutorial walks through Spring Boot 3’s built‑in @HttpExchange annotation, showing how to replace OpenFeign with a lightweight, declarative HTTP client using WebClient, complete with server and client code, configuration steps, and a runnable test example.

ITPUB
ITPUB
ITPUB
Mastering Spring Boot 3’s @HttpExchange for Declarative HTTP Calls

1. Origin

Spring Boot 3, released at the end of last year, introduces the @HttpExchange annotation, providing a native way to declare HTTP calls without relying on third‑party libraries such as OpenFeign.

2. Usage Example – Server

First, create a simple Spring Boot project named server that exposes a test endpoint:

@RestController
public class HelloController {

    @GetMapping("/server/hello")
    public String hello(String name) {
        return "hello " + name;
    }
}

3. Usage Example – Client

Next, create a second project called client. In addition to the standard web starter, add the reactive-web starter because @HttpExchange is built on top of WebClient:

Define a declarative interface for the remote call:

@HttpExchange("/server")
public interface ToDoService {
    @GetExchange("/hello")
    String hello(@RequestParam String name);
}

The annotations map directly to familiar Spring MVC ones: @HttpExchange works like @RequestMapping, @GetExchange like @GetMapping, and there are equivalents such as @PostExchange, @DeleteExchange, @PatchExchange, and @PutExchange. Parameters must be annotated with @RequestParam, similar to OpenFeign.

4. Configuration

To enable the declarative client, configure a WebClient bean and a proxy factory:

@Configuration
public class WebConfig {
    @Bean
    WebClient webClient() {
        return WebClient.builder()
            .baseUrl("http://localhost:8080")
            .build();
    }

    @Bean
    ToDoService toDoService() {
        HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(
            WebClientAdapter.forClient(webClient()))
            .build();
        return factory.createClient(ToDoService.class);
    }
}

The first bean supplies the underlying WebClient, which defines the base URL and can be used to customize headers. The second bean creates a proxy implementation of the ToDoService interface using HttpServiceProxyFactory.

5. Consuming the Service

With the configuration in place, the client can inject ToDoService and call the remote method directly:

@SpringBootTest
class ClientApplicationTests {

    @Autowired
    ToDoService toDoService;

    @Test
    void contextLoads() {
        String hello = toDoService.hello("javaboy");
        System.out.println("hello = " + hello);
    }
}

This test demonstrates a straightforward, type‑safe HTTP call without any OpenFeign code.

6. Conclusion

Spring Boot 3’s @HttpExchange offers a clean, declarative alternative for inter‑service communication, leveraging the reactive WebClient stack and eliminating the need for external Feign dependencies.

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 BootwebclientOpenFeignHttpExchangeDeclarative HTTP
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.