Search in sources :

Example 1 with MissingServletRequestPartException

use of org.springframework.web.multipart.support.MissingServletRequestPartException 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 2 with MissingServletRequestPartException

use of org.springframework.web.multipart.support.MissingServletRequestPartException in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleMissingServletRequestPartException.

@Test
public void handleMissingServletRequestPartException() throws Exception {
    MissingServletRequestPartException ex = new MissingServletRequestPartException("name");
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertNotNull("No ModelAndView returned", mav);
    assertTrue("No Empty ModelAndView returned", mav.isEmpty());
    assertEquals("Invalid status code", 400, response.getStatus());
    assertTrue(response.getErrorMessage().contains("request part"));
    assertTrue(response.getErrorMessage().contains("name"));
    assertTrue(response.getErrorMessage().contains("not present"));
}
Also used : MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.Test)

Example 3 with MissingServletRequestPartException

use of org.springframework.web.multipart.support.MissingServletRequestPartException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method missingServletRequestPart.

@Test
public void missingServletRequestPart() {
    Exception ex = new MissingServletRequestPartException("partName");
    testException(ex);
}
Also used : MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) Test(org.junit.Test)

Aggregations

MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)3 Test (org.junit.Test)2 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1 BindException (org.springframework.validation.BindException)1 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)1 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 MissingPathVariableException (org.springframework.web.bind.MissingPathVariableException)1 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)1 ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)1 WebDataBinder (org.springframework.web.bind.WebDataBinder)1 RequestPart (org.springframework.web.bind.annotation.RequestPart)1 AsyncRequestTimeoutException (org.springframework.web.context.request.async.AsyncRequestTimeoutException)1 MultipartException (org.springframework.web.multipart.MultipartException)1