I have written some logic and custom validation in Validator initialize method. But when exception occurs , custom exception was thrown but override by ValidationException
eg. HV000032: Unable to initialize ........
public class CustomValidator implements ConstraintValidator<CustomClass, CharSequence> {
@Override
public void initialize(CustomClass annota) {
try {
//code
} catch (Exception e) {
throw new CustomException("custom error ", e); <-- this exception is override by javax.validation.ValidationException...
}
}
I want to get my custom exception and message . How can I implement that ...
In order to get your custom exception and message, you can catch the
ValidationException
thrown by the validation framework and extract your custom exception and message from it.Something like this should work.
the
isValid
method is the main method used by the validation framework to determine if an object is valid or not, and it is necessary to implement it in order to define the validation logic. Even if an exception is thrown in the initialize method, theisValid
method is still necessary and will be executed for valid objects.