JPA、Hibernate 验证器 - 不适用于字符串的电子邮件验证器

JPA, Hibernate validators - email validator not applying on String

我正在尝试弄清楚如何在我的控制器中验证 DTO

堆栈: Spring 引导 1.5.4,Spring 数据 JPA,Hibernate 验证 5,PostgreSQL

问题是我仍然收到关于缺少验证器的异常。

有什么线索吗?

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'. Check configuration for 'email'
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:227) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:308) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:242) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:616) ~[hibernate-validator-5.4.1.Final.jar:5.4.1.Final]

DTO

public class CreateUserDto {

    @NotEmpty
    private String name;

    @Email
    @NotEmpty
    private String email;

    private String password;

    private String address;

    private String phone;

    @NotNull
    private Role role;
}

控制器

@PostMapping(value = "/users", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public UserDto register(@Valid @RequestBody CreateUserDto user) {
    return userService.register(user);
}

我怀疑您使用的是 Bean Validation 2.0 和仅支持 BV 1.1 的 Hibernate Validator 5.4。 @Email 约束已添加到 2.0 中的规范中 - 在此之前,它只是 HV 特定的约束。

您可能添加了对验证的依赖-api 2.0.

所以:

  • 要么你回到 BV 1.1,你将需要使用 Hibernate Validator 特定的 @Email 约束(来自包 org.hibernate.validator.constraints);
  • 或者您将 HV 升级到 6.0(推荐)并保留 BV 2.0 和 javax.validation @Email 约束。

这是由于 Bean Validation 和 Hibernate Validator 之间的版本不匹配。

您只需要将 Hibernate 验证升级到最新版本。

行家

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>6.0.11.Final</version>
</dependency>

Gradle

compile group: 'org.hibernate', name: 'hibernate-validator', version: '6.0.10.Final'