I want to create custom message for my custom validation annotation. If this validation failed, it throws MethodArgumentNotValidException. Because I put @Valid for the @RequestBody.
I create @ControllerAdvice to handle/ override MethodArgumentNotValidException message. I have this enum error class separately. So every error that throws this kind of exception will throws the same message as Invalid Param
My question is is it possible to exclude my custom validation message and throws different message instead? How to make this @MyCustomAnnotation throws different exception message? Not fall under MethodArgumentNotValidException message.
My custom validation interface
@Documented
@Constraint(validatedBy = myValidator.class)
@Target({FIELD, METHOD, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckMyCode{
String message() default "{my custom string message}";
Class<?> [] groups() default {};
Class<? extends Payload>[] payload() default {};
}
My controller advice method to handle all MethodArgumentNotValidException exception
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception, HttpHeaders headers, HttpStatus status, WebRequest request){
CustomClassError<Object> error = CustomClassError.failure(ErrCode.INVALID_PARAM);
return new ResponseEntity<Object>(error, new HttpHeaders(), HttpStatus.OK);
}