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:
@@ -27,12 +27,14 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.DecodingException;
|
||||
import org.springframework.core.codec.Hints;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
@@ -49,7 +51,12 @@ import org.springframework.http.server.RequestPath;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.support.WebExchangeDataBinder;
|
||||
import org.springframework.web.reactive.function.BodyExtractor;
|
||||
import org.springframework.web.reactive.function.BodyExtractors;
|
||||
import org.springframework.web.reactive.function.UnsupportedMediaTypeException;
|
||||
@@ -220,6 +227,33 @@ class DefaultServerRequest implements ServerRequest {
|
||||
.onErrorMap(DecodingException.class, DECODING_MAPPER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Mono<T> bind(Class<T> bindType, Consumer<WebDataBinder> dataBinderCustomizer) {
|
||||
Assert.notNull(bindType, "BindType must not be null");
|
||||
Assert.notNull(dataBinderCustomizer, "DataBinderCustomizer must not be null");
|
||||
|
||||
return Mono.defer(() -> {
|
||||
WebExchangeDataBinder dataBinder = new WebExchangeDataBinder(null);
|
||||
dataBinder.setTargetType(ResolvableType.forClass(bindType));
|
||||
dataBinderCustomizer.accept(dataBinder);
|
||||
|
||||
ServerWebExchange exchange = exchange();
|
||||
return dataBinder.construct(exchange)
|
||||
.then(dataBinder.bind(exchange))
|
||||
.then(Mono.defer(() -> {
|
||||
BindingResult bindingResult = dataBinder.getBindingResult();
|
||||
if (bindingResult.hasErrors()) {
|
||||
return Mono.error(new BindException(bindingResult));
|
||||
}
|
||||
else {
|
||||
T result = (T) bindingResult.getTarget();
|
||||
return Mono.justOrEmpty(result);
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> attributes() {
|
||||
return this.exchange.getAttributes();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -52,6 +53,7 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.cors.reactive.CorsUtils;
|
||||
import org.springframework.web.reactive.function.BodyExtractor;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -1044,6 +1046,16 @@ public abstract class RequestPredicates {
|
||||
return this.request.bodyToFlux(typeReference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Mono<T> bind(Class<T> bindType) {
|
||||
return this.request.bind(bindType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Mono<T> bind(Class<T> bindType, Consumer<WebDataBinder> dataBinderCustomizer) {
|
||||
return this.request.bind(bindType, dataBinderCustomizer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> attributes() {
|
||||
return this.attributes;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -48,6 +48,8 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.reactive.function.BodyExtractor;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebSession;
|
||||
@@ -197,6 +199,31 @@ public interface ServerRequest {
|
||||
*/
|
||||
<T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference);
|
||||
|
||||
/**
|
||||
* Bind to this request and return an instance of the given type.
|
||||
* @param bindType the type of class to bind this request to
|
||||
* @param <T> the type to bind to
|
||||
* @return a mono containing either a constructed and bound instance of
|
||||
* {@code bindType}, or a {@link BindException} in case of binding errors
|
||||
* @since 6.1
|
||||
*/
|
||||
default <T> Mono<T> bind(Class<T> bindType) {
|
||||
return bind(bindType, dataBinder -> {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind to this request and return an instance of the given type.
|
||||
* @param bindType the type of class to bind this request to
|
||||
* @param dataBinderCustomizer used to customize the data binder, e.g. set
|
||||
* (dis)allowed fields
|
||||
* @param <T> the type to bind to
|
||||
* @return a mono containing either a constructed and bound instance of
|
||||
* {@code bindType}, or a {@link BindException} in case of binding errors
|
||||
* @since 6.1
|
||||
*/
|
||||
<T> Mono<T> bind(Class<T> bindType, Consumer<WebDataBinder> dataBinderCustomizer);
|
||||
|
||||
|
||||
/**
|
||||
* Get the request attribute value if present.
|
||||
* @param name the attribute name
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -25,6 +25,7 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -42,6 +43,7 @@ import org.springframework.http.server.RequestPath;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
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.ServerRequest;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -171,6 +173,16 @@ public class ServerRequestWrapper implements ServerRequest {
|
||||
return this.delegate.bodyToFlux(typeReference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Mono<T> bind(Class<T> bindType) {
|
||||
return this.delegate.bind(bindType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Mono<T> bind(Class<T> bindType, Consumer<WebDataBinder> dataBinderCustomizer) {
|
||||
return this.delegate.bind(bindType, dataBinderCustomizer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Object> attribute(String name) {
|
||||
return this.delegate.attribute(name);
|
||||
|
||||
Reference in New Issue
Block a user