Search in sources :

Example 1 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class RequestHeaderMethodArgumentResolverTests method dateConversion.

@Test
@SuppressWarnings("deprecation")
public void dateConversion() throws Exception {
    String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
    servletRequest.addHeader("name", rfc1123val);
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setConversionService(new DefaultFormattingConversionService());
    Object result = resolver.resolveArgument(paramDate, null, webRequest, new DefaultDataBinderFactory(bindingInitializer));
    assertTrue(result instanceof Date);
    assertEquals(new Date(rfc1123val), result);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method missingOptionalMultipartFile.

@Test
public void missingOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.empty(), actual);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(org.springframework.web.bind.annotation.RequestParam) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 3 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method getConfigurableWebBindingInitializer.

/**
	 * Return the {@link ConfigurableWebBindingInitializer} to use for
	 * initializing all {@link WebDataBinder} instances.
	 */
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(webFluxConversionService());
    initializer.setValidator(webFluxValidator());
    initializer.setMessageCodesResolver(getMessageCodesResolver());
    return initializer;
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer)

Example 4 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class DelegatingWebFluxConfigurationTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() throws Exception {
    delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));
    RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    ConversionService initializerConversionService = initializer.getConversionService();
    assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
    verify(webFluxConfigurer).configureMessageReaders(readers.capture());
    verify(webFluxConfigurer).extendMessageReaders(readers.capture());
    verify(webFluxConfigurer).getValidator();
    verify(webFluxConfigurer).getMessageCodesResolver();
    verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
    verify(webFluxConfigurer).addArgumentResolvers(any());
    assertSame(formatterRegistry.getValue(), initializerConversionService);
    assertEquals(7, readers.getValue().size());
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ConversionService(org.springframework.core.convert.ConversionService) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Example 5 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class ModelInitializerTests method basic.

@SuppressWarnings("unchecked")
@Test
public void basic() throws Exception {
    TestController controller = new TestController();
    Validator validator = mock(Validator.class);
    controller.setValidator(validator);
    List<SyncInvocableHandlerMethod> binderMethods = getBinderMethods(controller);
    List<InvocableHandlerMethod> attributeMethods = getAttributeMethods(controller);
    WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    BindingContext bindingContext = new InitBinderBindingContext(bindingInitializer, binderMethods);
    this.modelInitializer.initModel(bindingContext, attributeMethods, this.exchange).block(Duration.ofMillis(5000));
    WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
    assertEquals(Collections.singletonList(validator), binder.getValidators());
    Map<String, Object> model = bindingContext.getModel().asMap();
    assertEquals(5, model.size());
    Object value = model.get("bean");
    assertEquals("Bean", ((TestBean) value).getName());
    value = model.get("monoBean");
    assertEquals("Mono Bean", ((Mono<TestBean>) value).block(Duration.ofMillis(5000)).getName());
    value = model.get("singleBean");
    assertEquals("Single Bean", ((Single<TestBean>) value).toBlocking().value().getName());
    value = model.get("voidMethodBean");
    assertEquals("Void Method Bean", ((TestBean) value).getName());
    value = model.get("voidMonoMethodBean");
    assertEquals("Void Mono Method Bean", ((TestBean) value).getName());
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) BindingContext(org.springframework.web.reactive.BindingContext) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Test(org.junit.Test)

Aggregations

ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)26 Test (org.junit.Test)17 MethodParameter (org.springframework.core.MethodParameter)8 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)8 BindingContext (org.springframework.web.reactive.BindingContext)8 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)7 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)7 Before (org.junit.Before)6 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)6 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)5 Optional (java.util.Optional)4 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)4 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)3 ConversionService (org.springframework.core.convert.ConversionService)3 RequestParam (org.springframework.web.bind.annotation.RequestParam)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 Bean (org.springframework.context.annotation.Bean)2