Question

I used the addNode() method of ConstraintValidator.ContextConstraintViolationBuilder to add the error message to the customized path, but this method is deprecated. However, I get an AbstractMethodError when I use the new method addPropertyNode.

Is there some dependency that I am missing?

Below is the snippet of the error message:

java.lang.AbstractMethodError: 
    org.hibernate.validator.internal.engine.ConstraintValidatorContextImpl$ErrorBuilderImpl
       .addPropertyNode(Ljava/lang/String;)
          Ljavax/validation/ConstraintValidatorContext$ConstraintViolationBuilder
             $NodeBuilderCustomizableContext;

Below is a snippet of the validator for cross field validation with Hibernate Validator version 4.3.1-Final

import org.springframework.beans.*;
...

    private String first;
    private String second;

    public boolean isValid(Object value, ConstraintValidatorContext context) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(value);
        Object fieldObj = beanWrapper.getPropertyValue(first);
        Object verifyObj = beanWrapper.getPropertyValue(second);

        if (fieldObj.equals(verifyObj)) {
            return true;
        }
        else {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate("message")
                .addPropertyNode(fieldObj).addConstraintViolation();

            // ~ Depreciated, but works correctly~
            // context.buildConstraintViolationWithTemplate("message")
            //    .addNode(fieldObj).addConstraintViolation();

            return true;
        }
    }
Was it helpful?

Solution

The method addPropertyNode() has been added in Bean Validation 1.1 API. While you use the BV 1.1 API, you seem to use an earlier version of Hibernate Validator (4.x) which only implements Bean Validation 1.0. You should make sure to use Hibernate Validator 5.x which is the reference implementation of Bean Validation 1.1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top