Validating EJB / CDI Parameters With Java EE 7 📎
Method parameters of CDI managed beans and EJBs can be validated with the Bean Validation API:
import javax.ejb.Stateless;
import javax.validation.constraints.Min;
@Stateless
public class Boundary {
public String hey(@Min(10) String input) {
return "From server: " + input;
}
}
Passing a String with length lesser than 10 causes the following error:
Caused by: javax.validation.ConstraintViolationException: 1 constraint violation(s) occurred during method validation.
Constructor or Method: public java.lang.String com.airhacks.validation.Boundary.hey(java.lang.String)
Argument values: [too short]
Constraint violations:
(1) Kind: PARAMETER
parameter index: 0
message: must be greater than or equal to 10
root bean: com.airhacks.validation.Boundary@75ad6aef
property path: hey.arg0
constraint: @javax.validation.constraints.Min(message={javax.validation.constraints.Min.message}, payload=[], groups=[], value=10)
See you at Java EE Workshops at MUC Airport!