How to create a custom bean validation in javaee6
Tech-Today

How to create a custom bean validation in javaee6


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: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html.

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:
@Entity
@Table
public class User implements Serializeable {
@Email
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;

@Documented
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
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> {

@Override
public void initialize(Email constraintAnnotation) {

}

@Override
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 })




- Rest Testing With Arquillian In Jboss
This article will explain how we can automate REST web service testing using Arquillian and JBoss web server. First, you must create a javaee6 war (non-blank) project from jboss-javaee6 archetype. This should create a project with Member model, service,...

- How To Configure Mail Resource In Jboss 7.2 With A Sample Java Client
This tutorial assumes that you have the following installed on your local system: JBoss 7.2eclipse to run a projecta mail server, can be gmailIn this page we will summarize how to configure a mail dataSource in Jboss and create a JavaEE6 client to send...

- How To Send An Email In Glassfish Using Javamail Session Injection
This tutorial will teach us how to send an email in Glassfish using JavaMail session injection. It use gmail for sending the email. Steps: 1.) First we need to create a JavaMail session by, opening Glassfish admin: http://localhost:4848, and navigating...

- How To Load Property File From Glassfish Config's Folder
In seam you can define a component class in components.xml that will load the properties from the JBOSS_CONFIG folder. Add the ff lines: <component name="paramBean" class="com.ipil.PropertyBean" scope="application" auto-create="true" startup="true">...

- How To Call A Stateless Ejb From A Spring Rest Servlet Component
In my recent project I was ask to develop a rest servlet (using jersey) in a spring web project that would call an stateless EJB, where the business logic is.  This project is deployed on glassfish. After several hours I was able to figure out how...



Tech-Today








.