Java Reference
In-Depth Information
Table 6-2. @ValidateOnExecution Executable Types
Type
Description
NONE
Parameters and return types are not validated upon execution.
CONSTRUCTORS
Parameters and return types are validated as long as the executable is a constructor.
NON_GETTER_METHODS
Parameters and return types are validated as long as the executable is not a getter method.
GETTER_METHODS
Parameters and return types are validated as long as the executable is a getter method.
IMPLICIT
When placed on a class or interface, it causes validation to be skipped; otherwise,
the type attribute must match the type of executable being annotated. For instance,
if on a constructor, then the type attribute must be equal to CONSTRUCTORS .
ALL
All parameters and return types are validated for all executables.
If a constructor or method is annotated with the @ValidateOnExecution annotation, and the annotation's type
attribute is set to the executable type or IMPLICIT , then the executable will be validated upon execution. Otherwise,
the validation will be skipped. Similarly, if a class or interface is annotated with @ValidateOnExecution , and the
annotation's type attribute contains the executable type, then the class or interface will be validated. However, if the
class or interface annotation contains a type attribute set to IMPLICIT , then the validation will be skipped.
Let's take a look at one of the use cases for specification of the @ValidateOnExecution annotation. By default,
getter methods are not validated when method-level validation is being used. It is possible to change this default
behavior by annotating any getter methods that you want to validate with the @ValidateOnExecution annotation and
passing the appropriate type argument to the annotation. The following lines of code demonstrate how to enable a
getter method for validation:
@ValidateOnExecution(type=GETTER_METHODS)
public BigDecimal getTotal() {
return total;
}
It is also possible to specify more than one type for the @ValidateOnExecution annotation. To do so,
enclose the type values within curly brackets, as demonstrated in the following example. Suppose that we have
a service that it utilized for online catalog processing. The service class could include default impementations
for performing common tasks, such as processing an order. It is possible to validate these methods utilizing the
@ValidateOnExecution annotation, as follows:
public class CatalogService {
...
@ValidateOnExecution(type={javax.validation.executable.ExecutableType.NON_GETTER_METHODS,
javax.validation.executable.ExecutableType.CONSTRUCTORS})
public Order placeOrder(
@NotNull @Size(min = 3, max = 20) String customerNumber,
@NotNull @Valid Widget widget,
@Min(1) int quantity) {
Order order = new Order();
order.setCustomerNumber(customerNumber);
 
Search WWH ::




Custom Search