How to create a custom bean validation in JavaEE

Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint vali...

Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from  oracle javaee6 documentation:

If you try to annotated your bean field with @Email, nothing will happen :-)

I'll just write the most important part of the code, so I assume you already have a javaee6 web project created. I suggest you do that by using jboss javaee6 archetype.

For example we want a custom email validator, we will need at least 4 classes: bean, entity, @interface and the validator implementation. So let's start with the entity class, let's say we have  a User:
public class User implements Serializeable {
 private String email;
As you can see, we annotated the email field with @Email annotation. After that let's define the Email annotation:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
  ElementType.CONSTRUCTOR, ElementType.PARAMETER })
public @interface Email {

 String message() default "message.validation.invalidEmail";

 Class[] groups() default {};

 Class[] payload() default {};

 String regexp() default "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)";

This object contains the variables needed by a ConstraintValidator such as message, group, etc. And finally the implementation class:
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class EmailValidator implements ConstraintValidator<Email, String> {

 public void initialize(Email constraintAnnotation) {


 public boolean isValid(String value, ConstraintValidatorContext context) {
  try {
   new InternetAddress(value).validate();
  } catch (AddressException e) {
   return false;
  return true;

This class validates if a given value is email using the InternetAddress class. The EmailValidator implements ConstraintValidator with type parameters <interface, value>.

Note that you can initialize field in initialize() method, so later you can use these fields in isValid().

And that's it we now have a custom email validator. Just a reminder, in some of the class like the ones defined in hibernate you will notice that the interface contains: @Constraint(validatedBy = { }), just remove these property. This property specify which validator to use, without value it will not validate. Or you can specify the validator with this property: @Constraint(validatedBy = { EmailValidator.class })


javaee 1315854880061319307

Post a Comment Default Comments