0

My rest controller contains following post mapping :

@PostMapping
    public ResponseEntity<RespDTO> uploadDocument(@ModelAttribute  @Valid RequestDTO requestDTO,@RequestParam(value = "fileContent") MultipartFile fileContent) throws  ServiceException, URISyntaxException { }

ServiceExceptionn is a custom exception specific to my application.

The controller advice looks like below:

@ControllerAdvice
public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait {

    @Override
    public ResponseEntity process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {

     }

    @ExceptionHandler(FileTooLargeException.class)
    public ResponseEntity<ResponseDTO> handleFileTooLargeException(FileTooLargeException ex, @Nonnull NativeWebRequest request){
      }
}

application.yml contains below property :

spring:
  servlet:
    multipart:
      max-file-size: 2MB

If I call the rest api using file having size greater than 2MB, then I am getting below exception:

 io.undertow.server.handlers.form.MultiPartParserDefinition$FileTooLargeException: UT000054: The maximum size 5242880 for an individual file in a multipart request was exceeded

Issue that I am facing here is: the controller advice is not working as per expectation. handleFileTooLargeException - this method must get executed because it is annotated with ExceptionHandler mentioned with the specific exception type. But instead of that, the control goes into process method of the controller advice.

Not able to understand what I am missing here.

  • Does this answer your question? [How to set the max size of upload file](https://stackoverflow.com/questions/37540028/how-to-set-the-max-size-of-upload-file) – Jens May 02 '20 at 09:51

1 Answers1

0

You need ExceptionTranslator class to extend ResponseEntityExceptionHandler class to enable methods with @ExceptionHandler annotation

Priya Aggarwal
  • 1,254
  • 1
  • 10
  • 5