JPA, Hibernate validators - email validator not applying on String

Viewed 5055

I'm trying to figure out how to validate DTO in my controller

Stack: Spring boot 1.5.4, Spring Data JPA, Hibernate Validation 5, PostgreSQL

The problem is that I'm still getting exception about missing validators.

Any clues?

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;
}

Controller

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

I suspect you are using Bean Validation 2.0 with Hibernate Validator 5.4 which only supports BV 1.1. The @Email constraint was added to the spec in 2.0 - prior to that, it was only an HV specific constraint.

You probably added a dependency to validation-api 2.0.

So:

  • either you go back to BV 1.1 and you will need to use the Hibernate Validator specific @Email constraint (from the package org.hibernate.validator.constraints);
  • or you upgrade HV to 6.0 (which is recommended) and you keep BV 2.0 and the javax.validation @Email constraint.

This is due to a version mismatch between Bean Validation and Hibernate Validator.

You'll just need to upgrade your Hibernate validation to latest version.

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