3

I have a custom validation with annotations such as:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UniquePropertyValidator.class)
public @interface UniqueProperty {

    String message() default "sample message";

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

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

here's my validator:

public class UniquePropertyValidator extends JdbcDaoSupport implements ConstraintValidator<UniqueProperty, UniquePropertyClass> {

    @Inject public UniquePropertyValidator(DataSource dataSource) {
        setDataSource(dataSource);
    }

    @Override
    public void initialize(UniqueProperty property) {

    }

    @Override
    public boolean isValid(UniquePropertyClass propertyClass, ConstraintValidatorContext constraintValidatorContext) { return some boolean; }
}

and I am trying to use it as following:

SpringValidatorAdapter adapter = new SpringValidatorAdapter(validator);
BeanPropertyBindingResult result = new BeanPropertyBindingResult(propertyClass, propertyClass.getClass().getName());
adapter.validate(propertyClass, result, UniquePropertyClass.SomeValidationGroup.class);

if (result.hasErrors())
    throw new MethodArgumentNotValidException(null, result);

however I am getting this error:

HV000064: Unable to instantiate ConstraintValidator: class some.package.UniquePropertyValidator.

Now, I am well aware that my validator class does not have a default constructor without any method parameters. However, the following works with the exact annotation and validator:

public someMethod(@Validated(value = UniquePropertyClass.SomeValidationGroup.class) @RequestBody UniquePropertyClass propertyClass)

What I am asking is; is there a way to manually validate without the default constructor.

P.S. The reason why I cannot use @Validated (the working example above) is:

I have a @PathVariable (say id) and before validating @RequestBody UniquePropertyClass propertyClass object, I need to set the id of UniquePropertyClass object before validating it, since I could not find a way to bind the @PathVariable into @RequestBody field and validate on the fly with @Validated. So giving a hint on how to make this work would also be a perfectly acceptable answer.

Thanks!

Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78

1 Answers1

0

Create default constructor for the UniquePropertyValidator and add @Component annotation. Then just add

@Inject
private DataSource dataSource;

to inject the datasource rather than init it from Constructor. (See more for autowiring objects in validator)

UPDATE you can get the bean from context (see Spring get current ApplicationContext )

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Doesn't Spring always create a new instance with `adapter.validate(propertyClass, result, UniquePropertyClass.SomeValidationGroup.class);` instead of using the one that is present in the application context when I use `@Component`? Or will it only create a new one, since it could not find one in the application context? – Hasan Can Saral Apr 13 '17 at 09:21
  • It used the one that is in the application context instead of creating a new one. Thanks! – Hasan Can Saral Apr 13 '17 at 09:25