Search in sources :

Example 16 with WebDataBinder

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

the class InitBinderBindingContextTests method createBinderNullAttrName.

@Test
public void createBinderNullAttrName() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, null);
    assertNull(dataBinder.getDisallowedFields());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 17 with WebDataBinder

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

the class InitBinderBindingContextTests method createBinderWithAttrNameNoMatch.

@Test
public void createBinderWithAttrNameNoMatch() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, "invalidName");
    assertNull(dataBinder.getDisallowedFields());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 18 with WebDataBinder

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

the class RequestPartMethodArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
    boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
    String name = getPartName(parameter, requestPart);
    parameter = parameter.nestedIfOptional();
    Object arg = null;
    Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            WebDataBinder binder = binderFactory.createBinder(request, arg, name);
            if (arg != null) {
                validateIfApplicable(binder, parameter);
                if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                    throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                }
            }
            mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
        } catch (MissingServletRequestPartException ex) {
            if (isRequired) {
                throw ex;
            }
        } catch (MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingServletRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpInputMessage(org.springframework.http.HttpInputMessage) WebDataBinder(org.springframework.web.bind.WebDataBinder) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) RequestPart(org.springframework.web.bind.annotation.RequestPart) MultipartException(org.springframework.web.multipart.MultipartException) RequestPartServletServerHttpRequest(org.springframework.web.multipart.support.RequestPartServletServerHttpRequest) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

Example 19 with WebDataBinder

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

the class ExtendedServletRequestDataBinderTests method noUriTemplateVars.

@Test
public void noUriTemplateVars() throws Exception {
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);
    assertEquals(null, target.getName());
    assertEquals(0, target.getAge());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) TestBean(org.springframework.tests.sample.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) Test(org.junit.Test)

Example 20 with WebDataBinder

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

the class ExtendedServletRequestDataBinderTests method uriTemplateVarAndRequestParam.

@Test
public void uriTemplateVarAndRequestParam() throws Exception {
    request.addParameter("age", "35");
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "nameValue");
    uriTemplateVars.put("age", "25");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);
    assertEquals("nameValue", target.getName());
    assertEquals(35, target.getAge());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) Test(org.junit.Test)

Aggregations

WebDataBinder (org.springframework.web.bind.WebDataBinder)39 Test (org.junit.Test)23 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)15 BindingContext (org.springframework.web.reactive.BindingContext)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 MethodParameter (org.springframework.core.MethodParameter)5 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)5 TestBean (org.springframework.tests.sample.beans.TestBean)4 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)4 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)4 ConversionService (org.springframework.core.convert.ConversionService)3 HashMap (java.util.HashMap)2 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Before (org.junit.Before)1