Java Reference
In-Depth Information
Declaring Return Value Constraints
To declare constraints on method return values, apply constraint annotation(s) directly on the method. In the
following method named placeOrder , the return value will be validated to ensure that at least one order is placed:
@NotNull
public Order placeOrder(...) {
...
return order;
}
Similarly, a return value constraint can be placed on the constructor of a class to ensure that the object being
returned conforms to the specified constraints. For instance, to validate that the WidgetOrderingService in the
following example is valid, the constructor can be decorated with a constraint annotation, which validates the rules
to adhere as a valid service:
@ValidOrderingService
public WidgetOrderingService(){
...
}
Cascaded Validation
Marking a method or parameter with the @Valid annotation flags it for cascaded validation. This means that the
validation is recursive, in that any parameter or return value marked with @Valid will be validated. For example,
in the following example, any time that the placeOrder method is invoked, it will cause recursive validation to occur:
@NotNull @Valid
public Order placeOrder(...) {
...
return order;
}
Customizing Method, Constructor, or Getter Validation
It is possible to customize whether a method, constructor, or getter is validated. To do so the @ValidateOnExecution
annotation can be placed on a method, constructor, getter method, or type declaring the executable in order to
validate the annotated executable upon execution. The same effect can also be achieved via configuration within the
validation.xml file. First, let's take a look at how to make use of the @ValidateOnExecution annotation.
The @ValidateOnExecution annotation accepts an argument named type , which accepts an executable type that
is used to specify the override you want to perform. Table 6-2 lists the different executable types that can be passed to
the @ValidateOnExecution annotation.
 
Search WWH ::




Custom Search