I want to validate an email field in my project using @Email annotaion. But the problem is that my model class name is also Email. So it shows the exception 'Email cannot be converted to Annotation'. Is there any solutions for this (without changing the class name).
Here is my model class:
public class Email implements EntityMarker{
@NotEmpty
@Email
private String emailId;
@NotEmpty
@Size(max = 15, message = "Your name must be less than 15 characters")
private String name;
}
@Email
validator annotation belongs to hibernate-validator.
When you are using only @Email
the IDE will not be able to think that whether this is hibernate-validator or your class name.
So use the validator like this:
public class Email implements EntityMarker{
@NotEmpty
@org.hibernate.validator.constraints.Email
private String emailId;
..//do whatever you want
}
For using @Email
annotation please include hibernate-validator dependency in your pom.xml file.
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
Please let me know if it helps.