BindingContext implements TypeConverter
This commit is contained in:
@@ -15,15 +15,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.web.reactive.result.method;
|
package org.springframework.web.reactive.result.method;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.beans.SimpleTypeConverter;
|
|
||||||
import org.springframework.beans.TypeConverter;
|
import org.springframework.beans.TypeConverter;
|
||||||
|
import org.springframework.beans.TypeMismatchException;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.validation.support.BindingAwareModelMap;
|
import org.springframework.validation.support.BindingAwareModelMap;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.WebExchangeDataBinder;
|
import org.springframework.web.bind.WebExchangeDataBinder;
|
||||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
|
||||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
@@ -34,13 +36,13 @@ import org.springframework.web.server.ServerWebExchange;
|
|||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public class BindingContext {
|
public class BindingContext implements TypeConverter {
|
||||||
|
|
||||||
private final ModelMap model = new BindingAwareModelMap();
|
private final ModelMap model = new BindingAwareModelMap();
|
||||||
|
|
||||||
private final WebBindingInitializer initializer;
|
private final WebBindingInitializer initializer;
|
||||||
|
|
||||||
private final TypeConverter typeConverter;
|
private final WebDataBinder simpleValueDataBinder;
|
||||||
|
|
||||||
|
|
||||||
public BindingContext() {
|
public BindingContext() {
|
||||||
@@ -49,21 +51,10 @@ public class BindingContext {
|
|||||||
|
|
||||||
public BindingContext(WebBindingInitializer initializer) {
|
public BindingContext(WebBindingInitializer initializer) {
|
||||||
this.initializer = initializer;
|
this.initializer = initializer;
|
||||||
this.typeConverter = initSimpleTypeConverter(initializer);
|
this.simpleValueDataBinder = new WebExchangeDataBinder(null);
|
||||||
}
|
if (initializer != null) {
|
||||||
|
initializer.initBinder(this.simpleValueDataBinder);
|
||||||
private static SimpleTypeConverter initSimpleTypeConverter(WebBindingInitializer initializer) {
|
|
||||||
SimpleTypeConverter converter = new SimpleTypeConverter();
|
|
||||||
if (initializer instanceof ConfigurableWebBindingInitializer) {
|
|
||||||
converter.setConversionService(
|
|
||||||
((ConfigurableWebBindingInitializer) initializer).getConversionService());
|
|
||||||
}
|
}
|
||||||
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);
|
return new WebExchangeDataBinder(target, objectName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Mono<WebExchangeDataBinder> initBinder(WebExchangeDataBinder dataBinder, ServerWebExchange exchange) {
|
protected Mono<WebExchangeDataBinder> initBinder(WebExchangeDataBinder binder, ServerWebExchange exchange) {
|
||||||
return Mono.just(dataBinder);
|
return Mono.just(binder);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException {
|
||||||
* Return a {@link TypeConverter} for converting plain parameter values.
|
return this.simpleValueDataBinder.convertIfNecessary(value, requiredType);
|
||||||
* This is a shortcut for:
|
}
|
||||||
* <pre>
|
|
||||||
* new WebDataBinder(null).getTypeConverter();
|
public <T> T convertIfNecessary(Object value, Class<T> requiredType, MethodParameter methodParam)
|
||||||
* </pre>
|
throws TypeMismatchException {
|
||||||
*/
|
|
||||||
public TypeConverter getTypeConverter() {
|
return this.simpleValueDataBinder.convertIfNecessary(value, requiredType, methodParam);
|
||||||
return this.typeConverter;
|
}
|
||||||
|
|
||||||
|
public <T> T convertIfNecessary(Object value, Class<T> requiredType, Field field)
|
||||||
|
throws TypeMismatchException {
|
||||||
|
|
||||||
|
return this.simpleValueDataBinder.convertIfNecessary(value, requiredType, field);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.beans.ConversionNotSupportedException;
|
import org.springframework.beans.ConversionNotSupportedException;
|
||||||
import org.springframework.beans.TypeConverter;
|
|
||||||
import org.springframework.beans.TypeMismatchException;
|
import org.springframework.beans.TypeMismatchException;
|
||||||
import org.springframework.beans.factory.config.BeanExpressionContext;
|
import org.springframework.beans.factory.config.BeanExpressionContext;
|
||||||
import org.springframework.beans.factory.config.BeanExpressionResolver;
|
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) {
|
private Object applyConversion(Object value, MethodParameter parameter, BindingContext bindingContext) {
|
||||||
try {
|
try {
|
||||||
TypeConverter typeConverter = bindingContext.getTypeConverter();
|
value = bindingContext.convertIfNecessary(value, parameter.getParameterType(), parameter);
|
||||||
value = typeConverter.convertIfNecessary(value, parameter.getParameterType(), parameter);
|
|
||||||
}
|
}
|
||||||
catch (ConversionNotSupportedException ex) {
|
catch (ConversionNotSupportedException ex) {
|
||||||
throw new ServerErrorException("Conversion not supported.", parameter, ex);
|
throw new ServerErrorException("Conversion not supported.", parameter, ex);
|
||||||
|
|||||||
Reference in New Issue
Block a user