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.

de_xtr
  • 882
  • 2
  • 10
  • 21

1 Answers1

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 hapen 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.

Community
  • 1
  • 1
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • 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. @luboskrnac Please check the update 1. – de_xtr Oct 02 '15 at 15:12
  • I was wrong, before and corrected my answer. You want to use ErrorController. – luboskrnac Oct 02 '15 at 15:27
  • Thanks for pointing it out. I have updated my post with the final solution I'm using. – de_xtr Oct 02 '15 at 15:57
  • @luboskrnac what if I use @RestController? I don't want to return page - just object – gstackoverflow Nov 20 '17 at 20:15
  • @gstackoverflow AFAIK, it shouldn't make a difference. – luboskrnac Nov 21 '17 at 08:30
  • @luboskrnac https://stackoverflow.com/a/47401986/2674303 - I found out the root cause. Tomcat sucks – gstackoverflow Nov 21 '17 at 09:19