use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessor method resolveArgument.
/**
* Throws MethodArgumentNotValidException if validation fails.
* @throws HttpMessageNotReadableException if {@link RequestBody#required()}
* is {@code true} and there is no body content or if there is no suitable
* converter to read the content with.
*/
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
parameter = parameter.nestedIfOptional();
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
String name = Conventions.getVariableNameForParameter(parameter);
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
return adaptArgumentIfNecessary(arg, parameter);
}
use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.
the class ServletModelAttributeMethodProcessor method createAttributeFromRequestValue.
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>The default implementation converts only if there a registered
* {@link Converter} that can perform the conversion.
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute, never {@code null}
* @param methodParam the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(methodParam);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
}
}
return null;
}
use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.
the class AbstractRequestAttributesArgumentResolverTests method resolveOptional.
@Test
public void resolveOptional() throws Exception {
WebDataBinder dataBinder = new WebRequestDataBinder(null);
dataBinder.setConversionService(new DefaultConversionService());
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder);
MethodParameter param = initMethodParameter(3);
Object actual = testResolveArgument(param, factory);
assertNotNull(actual);
assertEquals(Optional.class, actual.getClass());
assertFalse(((Optional) actual).isPresent());
Foo foo = new Foo();
this.webRequest.setAttribute("foo", foo, getScope());
actual = testResolveArgument(param, factory);
assertNotNull(actual);
assertEquals(Optional.class, actual.getClass());
assertTrue(((Optional) actual).isPresent());
assertSame(foo, ((Optional) actual).get());
}
use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.
the class InitBinderBindingContextTests method createBinderWithGlobalInitialization.
@Test
public void createBinderWithGlobalInitialization() throws Exception {
ConversionService conversionService = new DefaultFormattingConversionService();
bindingInitializer.setConversionService(conversionService);
ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
BindingContext context = createBindingContext("initBinder", WebDataBinder.class);
WebDataBinder dataBinder = context.createDataBinder(exchange, null, null);
assertSame(conversionService, dataBinder.getConversionService());
}
use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.
the class InitBinderBindingContextTests method createBinderTypeConversion.
@Test
public void createBinderTypeConversion() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("/path?requestParam=22").toExchange();
ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
this.argumentResolvers.add(new RequestParamMethodArgumentResolver(null, adapterRegistry, false));
BindingContext context = createBindingContext("initBinderTypeConversion", WebDataBinder.class, int.class);
WebDataBinder dataBinder = context.createDataBinder(exchange, null, "foo");
assertNotNull(dataBinder.getDisallowedFields());
assertEquals("requestParam-22", dataBinder.getDisallowedFields()[0]);
}
Aggregations