How to handle maximum file size Exception in Spring Boot?
Asked Answered
C

1

11

I am using Spring Boot v1.2.5 for creating a REST Application. While uploading images, I have a check for maximum file size , which is provided the property :

multipart.maxFileSize= 128KB

in application.properties. This facility is provided by Spring Boot itself. Now the check is working properly. The question is, how do I handle the exception and return a message to the user that he can understand ?

Update 1----------

I wrote a method within my Controller, where I intend to handle the MultipartException, using @ExceptionHandler. It does not seem to work.

This is my code :

@ExceptionHandler(MultipartException.class)
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
public ApplicationErrorDto handleMultipartException(MultipartException exception){
    ApplicationErrorDto applicationErrorDto =  new ApplicationErrorDto();
    applicationErrorDto.setMessage("File size exceeded");
    LOGGER.error("File size exceeded",exception);
    return applicationErrorDto;
}

Update 2----------

After @luboskrnac pointed it out, I have managed to come up with a solution. We can use ResponseEntityExceptionHandler here to handle this particular case. I believe, we could also have used DefaultHandlerExceptionResolver, but ResponseEntityExceptionHandler will allow us to return a ResponseEntity, as opposed to the former, the methods of which will return ModelAndView. I have not tried it though.

This is the final code that I'm using to handle the MultipartException :

@ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

private static final Logger LOGGER = Logger.getLogger(CustomResponseEntityExceptionHandler.class);

@ExceptionHandler(MultipartException.class)
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
@ResponseBody
public ApplicationErrorDto handleMultipartException(MultipartException exception){
    ApplicationErrorDto applicationErrorDto =  new ApplicationErrorDto();
    applicationErrorDto.setMessage("File size exceeded");
    LOGGER.error("File size exceeded",exception);
    return applicationErrorDto;
}
}

I am using Swagger for developing/documenting REST Apis. This is the response upon uploading a file that exceeds the size. enter image description here Thanks.

Convince answered 30/9, 2015 at 13:27 Comment(0)
C
5

Spring Boot docs says:

You can also use regular Spring MVC features like @ExceptionHandler methods and @ControllerAdvice. The ErrorController will then pick up any unhandled exceptions.

As MultipartException seem to happen before @Controller/@ExceptionHandler/@ControllerAdvice features comes into play, you should use ErrorController to handle it.

BTW, I found this thread in the meantime. You may want to take a look.

Cobblestone answered 1/10, 2015 at 14:44 Comment(6)
I tried doing that. I tried handling the exception locally within my controller using ExceptionHandler, instead of a global wrapper using ControllerAdvice, as instructed in the docs you had shared. I'm trying to handle MultipartException in the method. It does not seem to work. @Cobblestone Please check the update 1.Convince
I was wrong, before and corrected my answer. You want to use ErrorController.Cobblestone
Thanks for pointing it out. I have updated my post with the final solution I'm using.Convince
@Cobblestone what if I use @RestController? I don't want to return page - just objectKarilynn
@Karilynn AFAIK, it shouldn't make a difference.Cobblestone
@Cobblestone https://mcmap.net/q/1158932/-tomcat-exceptionhasndler-doesn-39-t-work-for-multipartexception-but-works-correct-for-illegalargumentexception - I found out the root cause. Tomcat sucksKarilynn

© 2022 - 2024 — McMap. All rights reserved.