Versioning support in WebTestClient controller setup

See gh-34919
This commit is contained in:
rstoyanchev
2025-05-19 09:31:37 +01:00
parent 3095219479
commit 094e653746
4 changed files with 62 additions and 17 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.Validator;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.ApiVersionConfigurer;
import org.springframework.web.reactive.config.BlockingExecutionConfigurer;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
@@ -118,6 +119,12 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
return this;
}
@Override
public WebTestClient.ControllerSpec apiVersioning(Consumer<ApiVersionConfigurer> configurer) {
this.configurer.versionConsumer = configurer;
return this;
}
@Override
public DefaultControllerSpec viewResolvers(Consumer<ViewResolverRegistry> consumer) {
this.configurer.viewResolversConsumer = consumer;
@@ -168,6 +175,8 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
private @Nullable Validator validator;
private @Nullable Consumer<ApiVersionConfigurer> versionConsumer;
private @Nullable Consumer<ViewResolverRegistry> viewResolversConsumer;
private @Nullable Consumer<BlockingExecutionConfigurer> executionConsumer;
@@ -219,6 +228,13 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
return this.validator;
}
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
if (this.versionConsumer != null) {
this.versionConsumer.accept(configurer);
}
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
if (this.viewResolversConsumer != null) {
@@ -48,6 +48,7 @@ import org.springframework.validation.Validator;
import org.springframework.web.client.ApiVersionFormatter;
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.ApiVersionConfigurer;
import org.springframework.web.reactive.config.BlockingExecutionConfigurer;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.PathMatchConfigurer;
@@ -346,6 +347,12 @@ public interface WebTestClient {
*/
ControllerSpec validator(Validator validator);
/**
* Configure API versioning for mapping requests to controller methods.
* @since 7.0
*/
ControllerSpec apiVersioning(Consumer<ApiVersionConfigurer> configurer);
/**
* Configure view resolution.
* @see WebFluxConfigurer#configureViewResolvers