JPA, Hibernate validators - email validator not applying on String
Asked Answered
E

2

5

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);
}
Euler answered 15/11, 2017 at 15:28 Comment(0)
S
7

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.
Snuck answered 15/11, 2017 at 15:37 Comment(3)
I have upgraded HV to 6.0 and it's working now, thanksEuler
I have HV 6.0.5 and BV 2.0 and receiving the error: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'.Deluca
Are you sure you don't have a HV 5.x around in the classpath? If not, please try to reproduce it with our test case template (github.com/hibernate/hibernate-test-case-templates/tree/master/…) and post on our issue tracker: hibernate.atlassian.net . Thanks.Snuck
S
1

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'
Sanitary answered 23/7, 2018 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.