Spring boot: unable to instantiate ConstraintValidator: Unsatisfied dependency

I've got the following DTO with its own custom constraint annotation and relative validator:
// ...

@Data
public class MyResource {
private String foo;
private Integer bar;

@Constraint(validatedBy = MyValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyResConstraint {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

public class MyValidator implements ConstraintValidator<MyResConstraint, Object> {
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}

}
}
// ...

@Data
public class MyResource {
private String foo;
private Integer bar;

@Constraint(validatedBy = MyValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyResConstraint {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

public class MyValidator implements ConstraintValidator<MyResConstraint, Object> {
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}

}
}
I've applied to a @Validated RestController class method, as follows:
@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> whatever(
@RequestBody @ModelAttribute @MyResConstraint
MyResource res
) {
@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> whatever(
@RequestBody @ModelAttribute @MyResConstraint
MyResource res
) {
When I try to call the POST mapping, I get the following error:
[Request processing failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.MyResource$MyValidator': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.MyResource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}]
[Request processing failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.MyResource$MyValidator': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.MyResource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}]
What am I doing wrong? Thanks!
1 Reply
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @elegos! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
Want results from more Discord servers?
Add your server