Custom ConstraintValidator can not init when first access at JPA entity

Description

Dear professor, thanks for read my confuse.

I had create a custom `ConstraintValidator`, code like those:

```
...
@Slf4j
public class PathValidator implements ConstraintValidator<Path, String> {

private final MlbService mlbService;

private final Set<String> catalogSet = new CopyOnWriteArraySet<>();
private final Set<String> extensionSet = new CopyOnWriteArraySet<>();

// path regex
private final Pattern pathPattern = Pattern.compile("^(\\w+)/(\\w)/(\\w)/([
w-]{36})\\.(
w+)$");

@Autowired
public PathValidator(MlbService mlbService) {
this.mlbService = mlbService;
}

@Override
public void initialize(Path constraintAnnotation) {
catalogSet.addAll(mlbService.minioSupportCatalog());
extensionSet.addAll(mlbService.minioSupportExtension());
}

@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
var matcher = pathPattern.matcher(s);
if (!matcher.find()) {
log.warn("Minio path {} is illegal", s);
return false;
}
var catalog = matcher.group(1);
var extension = matcher.group(5);
if (catalogSet.contains(catalog) && extensionSet.contains(extension)) {
return true;
} else {
log.warn("Minio path {} is illegal, catalog or extension illegal", s);
return false;
}
}
}
```

`MlbService` is a Spring Service Bean, i need inject this bean at constructor because it had parse some config from yaml.

JSR 303 annotation is named `@Path`.

```
...
@Constraint(validatedBy = {PathValidator.class})
...
public @interface Path {
...
}
```

But i had some problem.

The @Path is normal when i annotated it at method parameters or return value, but when i use it at JPA entity and repository interface is annotated with `@Validated`, and call the save method or others, then , i will get `HV000064: Unable to instantiate ConstraintValidator` and `NoSuchMethodException: ...PathValidator.<init>()`

I am very very confuse about it.

and i try to debug the source code, i found in normal case the `ValidatorFactory` is `SpringConstraintValidatorFactory`, but at abnormal case is `ConstraintValidatorFactoryImpl`, `ConstraintValidatorFactoryImpl` find no args constructor at ConstraintValidator define, so that get an NoSuchMethodException.

I had search this issue at google, but no useful information found, i am very very confuse about this, that bothers me the most.

And the attachment is the full releate code.

Thank you for reading this, I look forward to receiving your reply.

Environment

SpringBoot v2.5.3

Attachments

6
  • 12 Sep 2021, 05:13 AM
  • 12 Sep 2021, 05:07 AM
  • 12 Sep 2021, 05:07 AM
  • 12 Sep 2021, 05:05 AM
  • 12 Sep 2021, 05:05 AM
  • 12 Sep 2021, 05:05 AM

Activity

Show:

Marko Bekhta December 12, 2024 at 11:38 AM

Hey ,

I think that you’ve correctly identified the problem here. If the framework integration hasn’t correctly set the ConstraintValidatorFactory Hibernate Validator ends up using the default one (ConstraintValidatorFactoryImpl) which cannot inject beans into validators.
You would want to check, and make sure that Spring correctly configures Hibernate ORM and passes an instance of their SpringConstraintValidatorFactory so it can be picked up by ORM and eventually used by Hibernate Validator when performing validation of entities.

I will close the issue since there’s not much we can do here from the Hibernate Validator perspective.

Rejected

Details

Assignee

Reporter

Bug Testcase Reminder (view)

Bug reports should generally be accompanied by a test case!

Bug Testcase Reminder (edit)

Bug reports should generally be accompanied by a test case!

Participants

Marko Bekhta
seliote

Components

Affects versions

Priority

Created September 12, 2021 at 5:13 AM
Updated December 12, 2024 at 11:38 AM
Resolved December 12, 2024 at 11:38 AM