I'm doing some file upload through jQuery ajax and thus want to have a proper response in any case. I am now struggling with the size limit I set in the application.properties:
spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB
spring.http.multipart.enabled=false
I found many tutorials and even some SO answers, but none of them seem to work on my side. Here two examples:
https://www.mkyong.com/spring-boot/spring-boot-file-upload-example-ajax-and-rest/
Multipart File Upload:Size exceed exception in spring boot return JSON error message
I always get an empty response and in the log I see another exception (the ERROR is from my code to see if my handler comes into play:
2018-10-19 15:57:54.183 ERROR 12940 --- [http-nio-9002-exec-2] c.i.p.m.mvc.RestExceptionHandler : CAUSE: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (12698493) exceeds the configured maximum (5242880)
2018-10-19 15:57:54.185 WARN 12940 --- [http-nio-9002-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (12698493) exceeds the configured maximum (5242880)
Here is my code, the commented out parts are some variations I tried.
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);
@ExceptionHandler(MultipartException.class)
//@ExceptionHandler(MaxUploadSizeExceededException.class)
@ResponseBody
public String handleTooLargeFiles(HttpServletRequest request, Throwable ex) {
//public ResponseEntity<?> handleTooLargeFiles(HttpServletRequest request, Throwable ex) {
LOGGER.error("CAUSE: " + ex.getCause().getMessage());
return "File size exceeds the allowable limit! (5MB)";
//return new ResponseEntity("File size exceeds the allowable limit! (5MB)", HttpStatus.PAYLOAD_TOO_LARGE);
}
}
What am I doing wrong?