Skip to content

Commit aff43c2

Browse files
committed
BindingContext implements TypeConverter
1 parent bd69390 commit aff43c2

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/BindingContext.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
package org.springframework.web.reactive.result.method;
1717

18+
import java.lang.reflect.Field;
19+
1820
import reactor.core.publisher.Mono;
1921

20-
import org.springframework.beans.SimpleTypeConverter;
2122
import org.springframework.beans.TypeConverter;
23+
import org.springframework.beans.TypeMismatchException;
24+
import org.springframework.core.MethodParameter;
2225
import org.springframework.ui.ModelMap;
2326
import org.springframework.validation.support.BindingAwareModelMap;
2427
import org.springframework.web.bind.WebDataBinder;
2528
import org.springframework.web.bind.WebExchangeDataBinder;
26-
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
2729
import org.springframework.web.bind.support.WebBindingInitializer;
2830
import org.springframework.web.server.ServerWebExchange;
2931

@@ -34,13 +36,13 @@
3436
* @author Rossen Stoyanchev
3537
* @since 5.0
3638
*/
37-
public class BindingContext {
39+
public class BindingContext implements TypeConverter {
3840

3941
private final ModelMap model = new BindingAwareModelMap();
4042

4143
private final WebBindingInitializer initializer;
4244

43-
private final TypeConverter typeConverter;
45+
private final WebDataBinder simpleValueDataBinder;
4446

4547

4648
public BindingContext() {
@@ -49,21 +51,10 @@ public BindingContext() {
4951

5052
public BindingContext(WebBindingInitializer initializer) {
5153
this.initializer = initializer;
52-
this.typeConverter = initSimpleTypeConverter(initializer);
53-
}
54-
55-
private static SimpleTypeConverter initSimpleTypeConverter(WebBindingInitializer initializer) {
56-
SimpleTypeConverter converter = new SimpleTypeConverter();
57-
if (initializer instanceof ConfigurableWebBindingInitializer) {
58-
converter.setConversionService(
59-
((ConfigurableWebBindingInitializer) initializer).getConversionService());
54+
this.simpleValueDataBinder = new WebExchangeDataBinder(null);
55+
if (initializer != null) {
56+
initializer.initBinder(this.simpleValueDataBinder);
6057
}
61-
else if (initializer != null) {
62-
WebDataBinder dataBinder = new WebDataBinder(null);
63-
initializer.initBinder(dataBinder);
64-
converter.setConversionService(dataBinder.getConversionService());
65-
}
66-
return converter;
6758
}
6859

6960

@@ -96,19 +87,24 @@ protected WebExchangeDataBinder createBinderInstance(Object target, String objec
9687
return new WebExchangeDataBinder(target, objectName);
9788
}
9889

99-
protected Mono<WebExchangeDataBinder> initBinder(WebExchangeDataBinder dataBinder, ServerWebExchange exchange) {
100-
return Mono.just(dataBinder);
90+
protected Mono<WebExchangeDataBinder> initBinder(WebExchangeDataBinder binder, ServerWebExchange exchange) {
91+
return Mono.just(binder);
10192
}
10293

103-
/**
104-
* Return a {@link TypeConverter} for converting plain parameter values.
105-
* This is a shortcut for:
106-
* <pre>
107-
* new WebDataBinder(null).getTypeConverter();
108-
* </pre>
109-
*/
110-
public TypeConverter getTypeConverter() {
111-
return this.typeConverter;
94+
public <T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException {
95+
return this.simpleValueDataBinder.convertIfNecessary(value, requiredType);
96+
}
97+
98+
public <T> T convertIfNecessary(Object value, Class<T> requiredType, MethodParameter methodParam)
99+
throws TypeMismatchException {
100+
101+
return this.simpleValueDataBinder.convertIfNecessary(value, requiredType, methodParam);
102+
}
103+
104+
public <T> T convertIfNecessary(Object value, Class<T> requiredType, Field field)
105+
throws TypeMismatchException {
106+
107+
return this.simpleValueDataBinder.convertIfNecessary(value, requiredType, field);
112108
}
113109

114110
}

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import reactor.core.publisher.Mono;
2323

2424
import org.springframework.beans.ConversionNotSupportedException;
25-
import org.springframework.beans.TypeConverter;
2625
import org.springframework.beans.TypeMismatchException;
2726
import org.springframework.beans.factory.config.BeanExpressionContext;
2827
import org.springframework.beans.factory.config.BeanExpressionResolver;
@@ -171,8 +170,7 @@ protected abstract Mono<Object> resolveName(String name, MethodParameter paramet
171170

172171
private Object applyConversion(Object value, MethodParameter parameter, BindingContext bindingContext) {
173172
try {
174-
TypeConverter typeConverter = bindingContext.getTypeConverter();
175-
value = typeConverter.convertIfNecessary(value, parameter.getParameterType(), parameter);
173+
value = bindingContext.convertIfNecessary(value, parameter.getParameterType(), parameter);
176174
}
177175
catch (ConversionNotSupportedException ex) {
178176
throw new ServerErrorException("Conversion not supported.", parameter, ex);

0 commit comments

Comments
 (0)