I'm developing a RESTful api using spring boot 2 and trying to upload a file with limit set in the yml file as servlet.multipart.max-file-size=2MB
.
As per my requirement any file size uploaded above 2MB need to inserted into the database with the request information (getUser()
) and the exception. I'm able to generate the exception message using @ExceptionHandler
but i do not get the request information (getUser()
). please advise.
I tried with WebRequest webRequest on the rest controller with webRequest.setAttribute but I get Null in the @ExceptionHandler
.
My Controller and Controller advice:
@PostMapping(path = "/sendEmail")
public ResponseEntity<EmailVO> sendEmail(@RequestPart(value = "file", required = false) MultipartFile[] uploadfile,
@RequestPart EmailVO emailVO,WebRequest webRequest, HttpServletRequest httprequest) {
webRequest.setAttribute("emailVO", emailVO, RequestAttributes.SCOPE_REQUEST);
EmailVO emailVO = myService.sendEmail(emailVO, uploadfile);
return new ResponseEntity<>(emailVO, HttpStatus.OK);
}
@ExceptionHandler(Exception.class)
public ExceptionResponse handleException(final Exception ex, final HttpServletRequest httprequest,WebRequest webrequest) {
if (ex instanceof MaxUploadSizeExceededException) {
EmailVO emailVO = (EmailVO) webrequest.getAttribute(EmailVO, RequestAttributes.SCOPE_REQUEST);
return new ExceptionResponse("Exceeds the limit for user "+emailVO.getUser,ex.getClass().getSimpleName(), httprequest.getRequestURI(),"Failure");
}
}
I'm unable to get the emailVO.getUser which is coming from the request controller