Add request binding to functional endpoints

This commit introduces form binding to ServerRequest in both WebMVC.fn
and WebFlux.fn's, in the form of a bind(Class) method.

Closes gh-25943
This commit is contained in:
Arjen Poutsma
2023-07-04 15:01:32 +02:00
parent a3e37597aa
commit d04d7b2e57
11 changed files with 598 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@@ -29,6 +29,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -48,6 +49,7 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.server.HandlerStrategies;
import org.springframework.web.reactive.function.server.ServerRequest;
@@ -219,6 +221,20 @@ public final class MockServerRequest implements ServerRequest {
return (Flux<S>) this.body;
}
@Override
@SuppressWarnings("unchecked")
public <T> Mono<T> bind(Class<T> bindType) {
Assert.state(this.body != null, "No body");
return (Mono<T>) this.body;
}
@Override
@SuppressWarnings("unchecked")
public <T> Mono<T> bind(Class<T> bindType, Consumer<WebDataBinder> dataBinderCustomizer) {
Assert.state(this.body != null, "No body");
return (Mono<T>) this.body;
}
@Override
public Map<String, Object> attributes() {
return this.attributes;