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!

Comments:

Adam, thanks for mentioning this feature!

Besides validating single parameters, you also can validate several parameters together (e.g. to ensure that both parameters of a method resetPassword(String password, String passwordRepeat) match) and method return values. All this also works for JAX-RS resource methods.

--Gunnar

Posted by Gunnar on June 28, 2013 at 09:33 PM CEST #

How can the same be done with JEE5

Posted by Jerry Stackhouse on July 01, 2013 at 12:41 AM CEST #

Thanks, It can be very helpful feature to avoid check input parameters

Posted by Vitaliy Kalayda on July 04, 2013 at 03:55 PM CEST #

Hello Jerry Stackhouse, i think you can do this in JEE 5 with interceptors

Posted by Vitaliy Kalayda on July 07, 2013 at 09:36 PM CEST #

Jerry,
you should be able to get the same parameter validation with interceptors. JEE5 does not come with BeanValidation but you can include (with oldies like JBoss 4.2.x as well) and following snippet should work: https://gist.github.com/kubamarchwicki/5959892

It's based on hibernate-validator 4.2

--Kuba

Posted by Kuba on July 10, 2013 at 12:16 AM CEST #

Dear Adam
We are using similar pattern to validate beans and ejb, works perfectly!!! But the problem arise when the remote capabilities was needed.... The EJB wasn't serializable and the RootBean of Constraint Validation give us problems...

Any suggestion?

please forget my english

Posted by Francisco Philip on July 23, 2013 at 07:07 PM CEST #

Just try to examine the given example. Didn't work. Using javax.validation.constraints.Min doesn't seem to work, hence supported types are: BigDecimal, BigInteger, byte, short, int, long, and their respective wrappers. Using public

String hey(@Size(min = 10) String input) {
return "From server: " + input;
}

seems to work. Could you clarify which Annotation you have used?

Posted by Vinh Nguyen on January 19, 2015 at 04:53 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License