SeamFramework.orgCommunity Documentation

Chapter 3. Bean Validation Integration

3.1. Validating HTTP requests
3.1.1. Validating entity body
3.1.2. Validating resource fields
3.1.3. Validating other method parameters
3.2. Validation configuration
3.3. Using validation groups

Bean Validation (JSR-303) is a specification introduced as a part of Java EE 6. It aims to provide a standardized way of validating the domain model across all application layers.

The Seam REST module follows the Bean Validation specification and the incoming HTTP requests can be validated with this standardized mechanism.

Firstly, enable the ValidationInterceptor in the beans.xml configuration file.


<interceptors>
    <class>org.jboss.seam.rest.validation.ValidationInterceptor</class>
</interceptors>

Then, enable validation of a particular method by decorating it with the @ValidateRequest annotation.

@PUT

@ValidateRequest
public void updateTask(Task incommingTask)
{
...
}

Now, the HTTP request's entity body (the incomingTask parameter) will be validated prior to invoking the method.

The JAX-RS specification allows path parameters, query parameters, matrix parameters, cookie parameters and headers to be passed as parameters of a resource method.


In order to prevent an oversized method signature when the number of parameters is too large, JAX-RS implementations provide implementations of the Parameter Object pattern . These objects aggregate multiple parameters into a single object, for example RESTEasy Form Object or Apache CXF Parameter Bean . These parameters can be validated by Seam REST. To trigger the validation, annotate the parameter with a javax.validation.Valid annotation.



In some cases, it is desired to have a specific group of constraints used for validation of web service parameters. These constraints are usually weaker than the default constraints of a domain model. Take partial updates as an example.

Consider the following example:


The Employee resource in the example above is not allowed to have the null value specified in any of its fields. Thus, the entire representation of a resource (including the department and related object graph) must be sent to update the resource.

When using partial updates, only values of modified fields are required to be sent within the update request, while the non-null values of the received object are updated. Therefore, two groups of constraints are needed: group for partial updates (including @Size and @Email, excluding @NotNull) and the default group (@NotNull).

A validation group is a simple Java interface:



Finally, the ValidationInterceptor is configured to validate the PartialUpdateGroup group only.