Search in sources :

Example 11 with WebDataBinder

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);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

Example 12 with WebDataBinder

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;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionService(org.springframework.core.convert.ConversionService) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) WebDataBinder(org.springframework.web.bind.WebDataBinder)

Example 13 with WebDataBinder

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());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 14 with WebDataBinder

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());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) ConversionService(org.springframework.core.convert.ConversionService) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 15 with WebDataBinder

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]);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Aggregations

WebDataBinder (org.springframework.web.bind.WebDataBinder)39 Test (org.junit.Test)23 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)15 BindingContext (org.springframework.web.reactive.BindingContext)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 MethodParameter (org.springframework.core.MethodParameter)5 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)5 TestBean (org.springframework.tests.sample.beans.TestBean)4 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)4 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)4 ConversionService (org.springframework.core.convert.ConversionService)3 HashMap (java.util.HashMap)2 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Before (org.junit.Before)1