JPA,Hibernate验证器 - 不在String上应用的电子邮件验证器

时间:2017-11-15 15:28:10

标签: spring-data-jpa bean-validation hibernate-validator

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

堆栈: Spring boot 1.5.4,Spring Data JPA,Hibernate Validation 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);
}

2 个答案:

答案 0 :(得分:5)

我怀疑你使用的是带有Hibernate Validator 5.4的Bean Validation 2.0,它只支持BV 1.1。 @Email约束被添加到2.0中的规范 - 在此之前,它只是一个HV特定约束。

您可能在validation-api 2.0中添加了依赖项。

所以:

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

答案 1 :(得分:1)

这是由于Bean验证和Hibernate Validator之间的版本不匹配所致。

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

Maven

<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'