SeamFramework.orgCommunity Documentation

Chapter 3. Bean Validation Integration

3.1. Validating HTTP requests
3.2. 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 integrates with the Bean Validation specification. This allows message bodies of HTTP requests to be validated using 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)
{
...
}

By default, the message body (the parameter with no annotations) is validated. If the object is valid, the web service method is executed. Otherwise, the ValidationException exception is thrown.

The ValidationException exception is a simple carrier of constraint violations found by the Bean Validation provider. The exception can be handled by an ExceptionMapper .

Seam REST comes with a built-in ValidationExceptionMapper which is registered by default. The exception mapper converts the ValidationException to an HTTP response with 400 (Bad request) status code. Furthermore, messages relevant to the violated constraints are sent within the message body of the HTTP response.


Besides the message body, the JAX-RS specification allows various parts of the HTTP request to be passed as method parameters. These parameters are usually HTTP form parameters, query parameters, path parameters, headers, etc. 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. For example RESTEasy Form Object or Apache CXF Parameter Bean. Seam REST validates these Parameter Objects by default.



In some cases, it is desired to have a specific group of constraints used to validate the web service parameters. These constraints are usually weaker compared to 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 the values of modified fields are required to be sent within the update request. Only the non-null values of the received object are updated. Therefore, two groups of constraints are needed: one for partial updates (including @Size and @Email, excluding @NotNull) and the default one (@NotNull).

A validation group is a simple Java interface:



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