Search in sources :

Example 11 with DefaultDataBinderFactory

use of org.springframework.web.bind.support.DefaultDataBinderFactory 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)

Aggregations

DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)11 Test (org.junit.Test)8 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)8 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)7 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)6 MethodParameter (org.springframework.core.MethodParameter)4 RequestParam (org.springframework.web.bind.annotation.RequestParam)4 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)3 Method (java.lang.reflect.Method)2 Optional (java.util.Optional)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LocalVariableTableParameterNameDiscoverer (org.springframework.core.LocalVariableTableParameterNameDiscoverer)1 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)1 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1