From 21887ad23bcfb1b534377b496571db25441ef986 Mon Sep 17 00:00:00 2001 From: mattbertolini Date: Mon, 20 Apr 2020 19:40:39 -0400 Subject: [PATCH] Allow override of data binder binding Motivation ---------- The Spring MVC ModelAttributeMethodProcessor includes many helpful extension methods that allow developers to extend and enhance the data binding capabilities of the class. Unfortunately, Spring WebFlux's equivalent class, the ModelAttributeMethodArgumentResolver, does not include these same extension methods. I am leveraging these extension methods, specifically the bindRequestParameters method, to provide valuable enhancements to my application. I would like to provide these same enhancements to the WebFlux portion of my application and am unable to do so at this time. I would like to update the WebFlux ModelAttributeMethodArgumentResolver to add the bindRequestParameters method. Modifications ------------- I created a new method called bindRequestParameters and encapsulated the WebExchangeDataBinder bind call inside of it. This method is marked as protected as it should only be used by children of this class. This change mirrors the behavior in the equivalent Spring MVC class (ModelAttributeMethodProcessor). Result ------ The WebFlux ModelAttributeMethodArgumentResolver can now accept alternative data binding implementations mirroring the Web MVC behavior. --- .../ModelAttributeMethodArgumentResolver.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java index 8022b3c26a5..60b07a42680 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java @@ -122,7 +122,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR return valueMono.flatMap(value -> { WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name); - return binder.bind(exchange) + return bindRequestParameters(binder, exchange) .doOnError(bindingResultMono::onError) .doOnSuccess(aVoid -> { validateIfApplicable(binder, parameter); @@ -147,6 +147,15 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR }); } + /** + * Extension point to bind the request to the target object. + * @param binder the data binder instance to use for the binding + * @param exchange the current request + */ + protected Mono bindRequestParameters(WebExchangeDataBinder binder, ServerWebExchange exchange) { + return binder.bind(exchange); + } + private Mono prepareAttributeMono(String attributeName, ResolvableType attributeType, BindingContext context, ServerWebExchange exchange) {