diff --git a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java index 4e1f4115b3..6ae22ec8db 100644 --- a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java +++ b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import org.jspecify.annotations.Nullable; @@ -69,7 +70,7 @@ public class HttpRequestValues { private final MultiValueMap cookies; - private @Nullable Object version; + private final @Nullable Object version; private final Map attributes; @@ -353,6 +354,16 @@ public class HttpRequestValues { return this; } + /** + * Provide access to every header configured so far with the option to + * add, replace, or remove values. + * @since 7.0 + */ + public Builder configureHeaders(Consumer consumer) { + consumer.accept(initHeaders()); + return this; + } + private HttpHeaders initHeaders() { this.headers = (this.headers != null ? this.headers : new HttpHeaders()); return this.headers; @@ -362,13 +373,27 @@ public class HttpRequestValues { * Add the given cookie name and values. */ public Builder addCookie(String name, String... values) { - this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>()); for (String value : values) { - this.cookies.add(name, value); + initCookies().add(name, value); } return this; } + /** + * Provide access to every cookie configured so far with the option to + * add, replace, or remove values. + * @since 7.0 + */ + public Builder configureCookies(Consumer> consumer) { + consumer.accept(initCookies()); + return this; + } + + private MultiValueMap initCookies() { + this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>()); + return this.cookies; + } + /** * Add the given request parameter name and values. *

When {@code "content-type"} is set to @@ -377,13 +402,27 @@ public class HttpRequestValues { * parameters. */ public Builder addRequestParameter(String name, String... values) { - this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>()); for (String value : values) { - this.requestParams.add(name, value); + initRequestParams().add(name, value); } return this; } + /** + * Provide access to every request parameter configured so far with the + * option to add, replace, or remove values. + * @since 7.0 + */ + public Builder configureRequestParams(Consumer> consumer) { + consumer.accept(initRequestParams()); + return this; + } + + private MultiValueMap initRequestParams() { + this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>()); + return this.requestParams; + } + /** * Add a part for a multipart request. The part may be: *

    @@ -420,11 +459,25 @@ public class HttpRequestValues { * @param value the attribute value */ public Builder addAttribute(String name, Object value) { - this.attributes = (this.attributes != null ? this.attributes : new HashMap<>()); - this.attributes.put(name, value); + initAttributes().put(name, value); return this; } + /** + * Provide access to every attribute configured so far with the option + * to add, replace, or remove values. + * @since 7.0 + */ + public Builder configureAttributes(Consumer> consumer) { + consumer.accept(initAttributes()); + return this; + } + + private Map initAttributes() { + this.attributes = (this.attributes != null ? this.attributes : new HashMap<>()); + return this.attributes; + } + /** * Set the request body as an Object to be serialized. */