Search in sources :

Example 1 with RequestPart

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

the class RequestParamMethodArgumentResolverTests method supportsParameter.

@Test
public void supportsParameter() {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) Test(org.junit.Test)

Example 2 with RequestPart

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

the class RequestPartMethodArgumentResolverTests method resolvePartArrayArgument.

@Test
public void resolvePartArrayArgument() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart part1 = new MockPart("requestPart", "Hello World 1".getBytes());
    MockPart part2 = new MockPart("requestPart", "Hello World 2".getBytes());
    request.addPart(part1);
    request.addPart(part2);
    request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null);
    assertTrue(result instanceof Part[]);
    Part[] parts = (Part[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], part1);
    assertEquals(parts[1], part2);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 3 with RequestPart

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

Aggregations

RequestPart (org.springframework.web.bind.annotation.RequestPart)3 Part (javax.servlet.http.Part)2 Test (org.junit.Test)2 MockPart (org.springframework.mock.web.test.MockPart)2 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MethodParameter (org.springframework.core.MethodParameter)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1 WebDataBinder (org.springframework.web.bind.WebDataBinder)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 MvcAnnotationPredicates.requestPart (org.springframework.web.method.MvcAnnotationPredicates.requestPart)1 MultipartException (org.springframework.web.multipart.MultipartException)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)1 RequestPartServletServerHttpRequest (org.springframework.web.multipart.support.RequestPartServletServerHttpRequest)1