Java 21/Spring 7.0.0-M7/Spring Boot 4.0.0-M1, I attempted to write test code for the API with versioning. When building WebTestClient and setting up apiVersioning is not enough, an exception indicates that it requires ApiVersionInserter. I need to use both configurations together to get it to work.

this.webTestClient = WebTestClient
        .bindToController(new GreetingController())
        .apiVersioning(apiVersionConfigurer ->
                apiVersionConfigurer.useRequestHeader("X-API-Version").setDefaultVersion("1.0"))
        .configureClient()
        .apiVersionInserter(ApiVersionInserter.builder()
                .useHeader("X-API-Version")
                .build())
        .build();

Comment From: rstoyanchev

I see your point but I don't see a good solution. The challenge is that WebTestClient involves a client and a server, both need to be configurable, and the strategies are different. So you might have a custom implementation that should be possible to plug in, or you might want to test sending a bad request.

Did you have a concrete proposal?

Comment From: hantsy

In my opinion, focus on the API versioning at the configuring client stage.

Keep with:


.configureClient()
        .apiVersionInserter(ApiVersionInserter.builder()
                .useHeader("X-API-Version")
                .build())

And provides consistent config with the clients(RestClient/RestTemplate, WebClient,...)

Comment From: rstoyanchev

That is what we have, but what about the server?

On the client side it's a simple version inserter and formatter, on the server side it's a version resolver, parser, validator (all that's on ApiVersionConfigurer). You can't take client config and fill in the server config.

Even if we did so for simple cases, you still need to be able to configure every setting on the server. What's being tested after all is the server, not the client.