1

I am using Spring Boot 1.5.3.RELEASE and using a Controller that takes a MultipartFile with some other information as arguments and returns a file.

Now I am facing the org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException when the file exceeds the maximum Sizes.

spring.http.multipart.maxFileSize=17728640
spring.http.multipart.maxRequestSize=17728640

This works well but i need a custom Response and actually the Exception is throwed only at server side before the method call.

Can anyone tell me how can I define a Custom Error Handler that handles this exception and response something like ResponseEntity.status(HttpStatus.CONFLICT).body("size_exceeded")

My Method:

@SuppressWarnings("rawtypes")
    @RequestMapping(value = "/{Id}/attachments", method = RequestMethod.POST)
    public ResponseEntity addTaskAttachment(@RequestParam("file") MultipartFile file, @PathVariable Long Id,
            @CurrentUser User currentUser) {
// Some code here
        ResponseEntity.status(HttpStatus.OK).body(attachmentAsByteArray);
    }
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
d4u7
  • 29
  • 5

1 Answers1

0

You are correct in your observation that an Exception Handler with @RestControllerAdvice wouldn't work for multi part exceptions and reason being MultipartFile parsing & validation step preceding the mapping resolver step.

As advised in first accepted answer by geoand for this SO question here , you need to define and register an ErrorController.

Also, note that as already mentioned in that answer , Spring Boot already defines a BasicErrorController that you can extend to add new content types to return a JSON etc ( since default is text/html ) by adding a new public method with @RequestMapping & @Produces .

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98