15
15
*/
16
16
package org .springframework .web .reactive .result .method ;
17
17
18
+ import java .lang .reflect .Field ;
19
+
18
20
import reactor .core .publisher .Mono ;
19
21
20
- import org .springframework .beans .SimpleTypeConverter ;
21
22
import org .springframework .beans .TypeConverter ;
23
+ import org .springframework .beans .TypeMismatchException ;
24
+ import org .springframework .core .MethodParameter ;
22
25
import org .springframework .ui .ModelMap ;
23
26
import org .springframework .validation .support .BindingAwareModelMap ;
24
27
import org .springframework .web .bind .WebDataBinder ;
25
28
import org .springframework .web .bind .WebExchangeDataBinder ;
26
- import org .springframework .web .bind .support .ConfigurableWebBindingInitializer ;
27
29
import org .springframework .web .bind .support .WebBindingInitializer ;
28
30
import org .springframework .web .server .ServerWebExchange ;
29
31
34
36
* @author Rossen Stoyanchev
35
37
* @since 5.0
36
38
*/
37
- public class BindingContext {
39
+ public class BindingContext implements TypeConverter {
38
40
39
41
private final ModelMap model = new BindingAwareModelMap ();
40
42
41
43
private final WebBindingInitializer initializer ;
42
44
43
- private final TypeConverter typeConverter ;
45
+ private final WebDataBinder simpleValueDataBinder ;
44
46
45
47
46
48
public BindingContext () {
@@ -49,21 +51,10 @@ public BindingContext() {
49
51
50
52
public BindingContext (WebBindingInitializer initializer ) {
51
53
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 );
60
57
}
61
- else if (initializer != null ) {
62
- WebDataBinder dataBinder = new WebDataBinder (null );
63
- initializer .initBinder (dataBinder );
64
- converter .setConversionService (dataBinder .getConversionService ());
65
- }
66
- return converter ;
67
58
}
68
59
69
60
@@ -96,19 +87,24 @@ protected WebExchangeDataBinder createBinderInstance(Object target, String objec
96
87
return new WebExchangeDataBinder (target , objectName );
97
88
}
98
89
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 );
101
92
}
102
93
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 );
112
108
}
113
109
114
110
}
0 commit comments