Search in sources :

Example 11 with ConfigurableWebBindingInitializer

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

the class RequestAttributeMethodArgumentResolverTests method resolveOptional.

@Test
public void resolveOptional() throws Exception {
    MethodParameter param = initMethodParameter(3);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    assertFalse(((Optional<?>) mono.block()).isPresent());
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    BindingContext bindingContext = new BindingContext(initializer);
    Foo foo = new Foo();
    this.exchange.getAttributes().put("foo", foo);
    mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    Optional<?> optional = (Optional<?>) mono.block();
    assertTrue(optional.isPresent());
    assertSame(foo, optional.get());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Optional(java.util.Optional) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 12 with ConfigurableWebBindingInitializer

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

the class RequestHeaderMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
    this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    this.bindingContext = new BindingContext(initializer);
    Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
    this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
    this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
    this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
    this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
    this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
    this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
    this.paramDate = new SynthesizingMethodParameter(method, 6);
    this.paramInstant = new SynthesizingMethodParameter(method, 7);
    this.paramMono = new SynthesizingMethodParameter(method, 8);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) Method(java.lang.reflect.Method) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Before(org.junit.Before)

Example 13 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-data-document-examples by spring-projects.

the class HandlerAdapterConfiguration method annotationMethodHandlerAdapter.

@Bean
public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter() {
    AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
    ActionInterceptor[] interceptors = new ActionInterceptor[] { datastoreInterceptor };
    //adapter.setActionInterceptors(interceptors);
    ConfigurableWebBindingInitializer wbi = new ConfigurableWebBindingInitializer();
    wbi.setValidator(getValidator());
    wbi.setConversionService(conversionService());
    adapter.setWebBindingInitializer(wbi);
    adapter.setMessageConverters(getMessageConverters());
    adapter.setOrder(-1);
    return adapter;
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ActionInterceptor(org.springframework.data.document.web.servlet.ActionInterceptor) AnnotationMethodHandlerAdapter(org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)

Example 14 with ConfigurableWebBindingInitializer

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

the class RequestHeaderMethodArgumentResolverTests method instantConversion.

@Test
public void instantConversion() 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(paramInstant, null, webRequest, new DefaultDataBinderFactory(bindingInitializer));
    assertTrue(result instanceof Instant);
    assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Instant(java.time.Instant) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 15 with ConfigurableWebBindingInitializer

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

the class RequestParamMethodArgumentResolverTests method optionalMultipartFileWithoutMultipartRequest.

@Test
public void optionalMultipartFileWithoutMultipartRequest() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    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)

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