Search in sources :

Example 1 with DefaultDataBinderFactory

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

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

the class InitBinderDataBinderFactoryTests method createFactory.

private WebDataBinderFactory createFactory(String methodName, Class<?>... parameterTypes) throws Exception {
    Object handler = new InitBinderHandler();
    Method method = handler.getClass().getMethod(methodName, parameterTypes);
    InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method);
    handlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
    handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null));
    handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
    return new InitBinderDataBinderFactory(Collections.singletonList(handlerMethod), this.bindingInitializer);
}
Also used : LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) Method(java.lang.reflect.Method) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory)

Example 3 with DefaultDataBinderFactory

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

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

the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.

@Test
public void resolveOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertTrue(result instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 5 with DefaultDataBinderFactory

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

the class RequestMappingHandlerAdapter method createInitBinderMethod.

private InvocableHandlerMethod createInitBinderMethod(Object bean, Method method) {
    InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(bean, method);
    binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
    binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
    binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
    return binderMethod;
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory)

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