I've a file upload validation that raises a BindException
instead of a MethodArgumentNotValidException
and I don't understand why.
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'patientProfileImageDTO' on field 'profileImage': rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@2840a305]; codes [CheckImageFormat.patientProfileImageDTO.profileImage,CheckImageFormat.profileImage,CheckImageFormat.org.springframework.web.multipart.MultipartFile,CheckImageFormat]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [patientProfileImageDTO.profileImage,profileImage]; arguments []; default message [profileImage]]; default message [Invalid image format (allowed: png, jpg, jpeg)]
My Controller is:
@PostMapping("/patient/image")
public ResponseEntity<?> updateProfileImage(@Validated PatientProfileImageDTO patientProfileImageDTO)
and this is the PatientProfileImageDTO
public class PatientProfileImageDTO {
@CheckImageFormat
@CheckImageSize
private MultipartFile profileImage;
public MultipartFile getProfileImage() {
return profileImage;
}
public void setProfileImage(MultipartFile profileImage) {
this.profileImage = profileImage;
}
}
the CheckFormatImage
and CheckImageSize
validators are correctly invoked.
I need to catch these errors in my:
@ControllerAdvice
public class ApiExceptionHandler {
ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, WebRequest request) {
...
}
}
I've other custom validation annotations in another part of my code and they work as intended.
I mean:
@OldPasswordMatch(message = "old password mismatch")
private String oldPassword;
This custom validation triggers a MethodArgumentNotValidException that what I want.
What's wrong with my code?
Thanks.
There is also a BindException
thrown by Spring MVC if an invalid object was created from the request parameters. MethodArgumentNotValidException
is already a subclass of BindException
.
These are actually intentionally different exceptions. @ModelAttribute, which is assumed by default if no other annotation is present, goes through data binding and validation, and raises BindException to indicate a failure with binding request properties or validating the resulting values. @RequestBody, on the other hand converts the body of the request via other converter, validates it and raises various conversion related exceptions or a MethodArgumentNotValidException if validation fails. In most cases a MethodArgumentNotValidException can be handled generically (eg via @ExceptionHandler method) while BindException is very often handled individually in each controller method.
You can process these errors separately or you can catch only the super class BindException
.
@ExceptionHandler(BindException.class)
protected ResponseEntity<Object> handleBindException(BindException ex) {
// ..
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.