Search in sources :

Example 21 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)

Example 22 with ConfigurableWebBindingInitializer

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

the class RequestParamMethodArgumentResolverTests method resolveOptionalParamValue.

@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() 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, Integer.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.empty(), result);
    this.request.addParameter("name", "123");
    result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.class, result.getClass());
    assertEquals(123, ((Optional) result).get());
}
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 23 with ConfigurableWebBindingInitializer

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

the class SessionAttributeMethodArgumentResolverTests 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();
    when(this.session.getAttribute("foo")).thenReturn(Optional.of(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 24 with ConfigurableWebBindingInitializer

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

the class RequestParamMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
    this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, true);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    this.bindContext = new BindingContext(initializer);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Before(org.junit.Before)

Example 25 with ConfigurableWebBindingInitializer

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

the class RequestMappingHandlerAdapterIntegrationTests method setup.

@Before
public void setup() throws Exception {
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setValidator(new StubValidator());
    List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
    customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.refresh();
    handlerAdapter = new RequestMappingHandlerAdapter();
    handlerAdapter.setWebBindingInitializer(bindingInitializer);
    handlerAdapter.setCustomArgumentResolvers(customResolvers);
    handlerAdapter.setApplicationContext(context);
    handlerAdapter.setBeanFactory(context.getBeanFactory());
    handlerAdapter.afterPropertiesSet();
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("POST");
    // Expose request to the current thread (for SpEL expressions)
    RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ArrayList(java.util.ArrayList) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

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