简体   繁体   中英

No validator can be found for String field

I have a custom interface:

@Target({ FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { MyCustomValidator.class })
@Documented
public @interface ValidData {

    String message() default EMPTY;

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

}

and I have a class with a field that has this validator attached:

public class RecoverData {

    @ValidData(groups = AfterDefaultGroup.class)
    private String data;

MyCustomValidator class has the following method:

public class MyCustomValidator implements ConstraintValidator<ValidData, RecoverData> {

    @Override
    public boolean isValid(RecoverData recoverData, ConstraintValidatorContext constraintValidatorContext) {
        //my custom logic regarding recoverData.getData()
        return true;
    }
}

Now when I try to run it, I'm getting the following error:

No validator could be found for constraint 'com.myPackage.ValidData' validating type 'java.lang.String'. Check configuration for 'data'

What could I do in this situation?

You have applied your custom validator for a String field and not a RecoverData type - so your custom validator should actually be:

public class MyCustomValidator implements ConstraintValidator<ValidData, String> {

  @Override
  public boolean isValid(String data, ConstraintValidatorContext context) {
    //my custom logic regarding recoverData.getData()
    return true;
  }

}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM