From aff43c29fdaea3790ca8c8eaa1e314e352f9e1a6 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 12 Oct 2016 09:01:15 -0400 Subject: [PATCH] BindingContext implements TypeConverter --- .../result/method/BindingContext.java | 54 +++++++++---------- ...tractNamedValueMethodArgumentResolver.java | 4 +- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/BindingContext.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/BindingContext.java index 42820303a8..894665d35d 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/BindingContext.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/BindingContext.java @@ -15,15 +15,17 @@ */ package org.springframework.web.reactive.result.method; +import java.lang.reflect.Field; + import reactor.core.publisher.Mono; -import org.springframework.beans.SimpleTypeConverter; import org.springframework.beans.TypeConverter; +import org.springframework.beans.TypeMismatchException; +import org.springframework.core.MethodParameter; import org.springframework.ui.ModelMap; import org.springframework.validation.support.BindingAwareModelMap; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebExchangeDataBinder; -import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.server.ServerWebExchange; @@ -34,13 +36,13 @@ import org.springframework.web.server.ServerWebExchange; * @author Rossen Stoyanchev * @since 5.0 */ -public class BindingContext { +public class BindingContext implements TypeConverter { private final ModelMap model = new BindingAwareModelMap(); private final WebBindingInitializer initializer; - private final TypeConverter typeConverter; + private final WebDataBinder simpleValueDataBinder; public BindingContext() { @@ -49,21 +51,10 @@ public class BindingContext { public BindingContext(WebBindingInitializer initializer) { this.initializer = initializer; - this.typeConverter = initSimpleTypeConverter(initializer); - } - - private static SimpleTypeConverter initSimpleTypeConverter(WebBindingInitializer initializer) { - SimpleTypeConverter converter = new SimpleTypeConverter(); - if (initializer instanceof ConfigurableWebBindingInitializer) { - converter.setConversionService( - ((ConfigurableWebBindingInitializer) initializer).getConversionService()); + this.simpleValueDataBinder = new WebExchangeDataBinder(null); + if (initializer != null) { + initializer.initBinder(this.simpleValueDataBinder); } - else if (initializer != null) { - WebDataBinder dataBinder = new WebDataBinder(null); - initializer.initBinder(dataBinder); - converter.setConversionService(dataBinder.getConversionService()); - } - return converter; } @@ -96,19 +87,24 @@ public class BindingContext { return new WebExchangeDataBinder(target, objectName); } - protected Mono initBinder(WebExchangeDataBinder dataBinder, ServerWebExchange exchange) { - return Mono.just(dataBinder); + protected Mono initBinder(WebExchangeDataBinder binder, ServerWebExchange exchange) { + return Mono.just(binder); } - /** - * Return a {@link TypeConverter} for converting plain parameter values. - * This is a shortcut for: - *
-	 * new WebDataBinder(null).getTypeConverter();
-	 * 
- */ - public TypeConverter getTypeConverter() { - return this.typeConverter; + public T convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { + return this.simpleValueDataBinder.convertIfNecessary(value, requiredType); + } + + public T convertIfNecessary(Object value, Class requiredType, MethodParameter methodParam) + throws TypeMismatchException { + + return this.simpleValueDataBinder.convertIfNecessary(value, requiredType, methodParam); + } + + public T convertIfNecessary(Object value, Class requiredType, Field field) + throws TypeMismatchException { + + return this.simpleValueDataBinder.convertIfNecessary(value, requiredType, field); } } diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueMethodArgumentResolver.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueMethodArgumentResolver.java index 7a2d3b5b13..d1e2a48eb4 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueMethodArgumentResolver.java @@ -22,7 +22,6 @@ import java.util.concurrent.ConcurrentHashMap; import reactor.core.publisher.Mono; import org.springframework.beans.ConversionNotSupportedException; -import org.springframework.beans.TypeConverter; import org.springframework.beans.TypeMismatchException; import org.springframework.beans.factory.config.BeanExpressionContext; import org.springframework.beans.factory.config.BeanExpressionResolver; @@ -171,8 +170,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle private Object applyConversion(Object value, MethodParameter parameter, BindingContext bindingContext) { try { - TypeConverter typeConverter = bindingContext.getTypeConverter(); - value = typeConverter.convertIfNecessary(value, parameter.getParameterType(), parameter); + value = bindingContext.convertIfNecessary(value, parameter.getParameterType(), parameter); } catch (ConversionNotSupportedException ex) { throw new ServerErrorException("Conversion not supported.", parameter, ex);