SeamFramework.orgCommunity Documentation

Seam REST Module

Reference Guide

3.0.0-SNAPSHOT


Introduction
1. Installation
1.1. Basics
1.2. Transitive dependencies
1.3. Registering JAX-RS components explicitly
2. Exception Handling
2.1. Seam Catch Integration
2.2. Declarative Exception Mapping
2.2.1. Programmatic configuration
2.2.2. XML configuration
2.2.3. Exception Mapping
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
4. Templating support
4.1. Creating JAX-RS responses using templates
4.1.1. Accessing the model
4.2. Builtin support for templating engines
4.2.1. FreeMarker
4.2.2. Apache Velocity
4.2.3. Pluggable support for templating engines
4.2.4. Selecting prefered templating engine
5. RESTEasy Client Framework Integration
5.1. Using RESTEasy Client Framework with Seam REST
5.2. Manual ClientRequest API
5.3. ClientExecutor configuration
6. Seam REST Dependencies

Seam REST is a lightweight module that aims to provide additional integration with technologies within the Java EE platform as well as third party technologies.

Seam REST is independent of CDI and JAX-RS implementations and thus fully portable between Java EE 6 environments.

The Seam REST module requires a Java EE 6 compliant server such as JBoss Application Server or GlassFish to run on.

The JAX-RS specification defines Exception Mapping Providers as a standard mechanism for treating Java exceptions. The Seam REST module comes with and alternative approach which is more consistent with the CDI programming model, easier to use and still portable.

Firstly, Seam Catch is plugged in which allows exceptions that occur in different parts of an application to be treated uniformly. Besides, a builtin exception handler is provided which enables simple exception-mapping rules to be defined declaratively.

Seam Catch can be used within the Seam REST module for dealing with exceptions. As a result, an exception that occurs during an invocation of a JAX-RS service is routed through the Catch exception handling mechanism which is similar to the CDI event bus and lets exception handling logic to be implemented in a loosely-coupled fashion.

The following code sample demonstrates a simple exception handler that converts the NoResultException exception to the 404 HTTP response.


Similarly to the CDI event bus, exceptions to be handled by a handler method can be filtered by qualifiers. In the example above, we are only interested in exceptions that occur in a JAX-RS service invocation. (As opposed to all exceptions of the given type that occur in the application - in the view layer for example.) Thus, the @RestRequest qualifier is used.

Catch integration is optional and only enabled when Catch libraries are available on classpath. For more information on Seam Catch, see Seam Catch reference documentation .

Often, exception-mapping rules are simple. Thus, they do not really need to be implemented in Java. Instead, declarative approach is more appropriate in these situations. The Seam REST module allows exception types to be bound to HTTP responses declaratively.

For each exception type, it is possible to specify the status code and the error message of the HTTP response. There are two ways of exception mapping configuration in Seam REST.

An alternative and more practical way of configuration is to use the Seam XML module to configure the ExceptionMappingConfiguration and ExceptionMapping classes in XML.

Firstly, the Seam XML module needs to be added to the application. If using maven, this can be done by specifying the following dependency:


For more information on the seam-xml module, refer to the Seam XML reference documentation Once the Seam XML module is added, specify the configuration in the seam-beans.xml file, located in the WEB-INF or META-INF folder of the web archive.


Furthermore, EL expressions can be used in message templates to provide dynamic and more descriptive error messages.


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 incomming 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)
{
...
}

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.


Seam REST allows HTTP responses to be created using templates. Instead of being bound to a particlar templating engine, Seam REST comes with support for multiple templating engines and support for others can be plugged in.

REST-based web services are often expected to return multiple representations of a resource. The templating support is useful for producing media formats such as XHTML and it can be also used instead of JAXB to produce domain-specific XML representations of a resource. Besides, almost any other representation of a resource can be described in a template.

To enable templating for a particular method, decorate the method with the @ResponseTemplate annotation. Path to a template file to be used for rendering is required.


The @ResponseTemplate annotation offers several other options. For example, it's possible for a method to offer multiple representations of a resource, each rendered using a different template. In this situation, the produces member of the @ResponseTemplate annotation is used to distinguish between produced media types.



There are several ways of accessing the domain data within a template.

Firstly, the object returned by the JAX-RS method is available under the "response" name by default. The object can be made available under a different name using the responseName member of the @ResponseTemplate annotation.


Secondly, every bean reachable via an EL expression is available within a template.


Note

Note that the syntax of the expression depends on the particular templating engine and most of the time differs from the syntax of EL expressions. For example, ${university.students} must be used instead of #{university.students} in a FreeMarker template.

Last but not least, the model can be populated programatically. In order to do that, inject the TemplatingModel bean and put the desired objects into the underlying data map. In the following example, the list of professors is available under the "professors" name.


Seam REST currently comes with builtin templating providers for FreeMarker and Apache Velocity.

The RESTEasy Client Framework is a framework for writing clients for REST-based web services. It reuses JAX-RS metadata for creating HTTP requests. For more information about the framework, refer to the project documentation .

Integration with the RESTEasy Client Framework is optional in Seam REST and only available when RESTEasy is available on classpath.

The Seam REST module depends on the following transitive dependencies at runtime.