SeamFramework.orgCommunity Documentation

Seam REST Module

Reference Guide

3.0.0.CR2


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. Annotation-based configuration
2.2.2. XML configuration
2.2.3. Declarative exception mapping processing
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. Built-in 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
6.1. Transitive Dependencies
6.2. Optional dependencies
6.2.1. Seam Catch
6.2.2. Seam Config
6.2.3. FreeMarker
6.2.4. Apache Velocity
6.2.5. RESTEasy

Seam REST is a lightweight module that provides additional integration of technologies within the Java EE platform as well as third party technologies.

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

The Seam REST module runs only on Java EE 6 compliant servers such as JBoss Application Server or GlassFish .

The JAX-RS specification defines the mechanism for exception mapping providers as the standard mechanism for Java exception handling. The Seam REST module comes with an alternative approach, which is more consistent with the CDI programming model. It is also easier to use and still remains portable.

The Seam REST module allows you to:

  • integrate with Seam Catch and thus handle exceptions that occur in different parts of an application uniformly;

  • define exception handling rules declaratively with annotations or XML.

Seam Catch handles exceptions within the Seam REST module: as result, an exception that occurs during an invocation of a JAX-RS service is routed through the Catch exception handling mechanism similar to the CDI event bus. This allows you to implement the exception handling logic in a loosely-coupled fashion.

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


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

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

Exception-mapping rules are often fairly simple. Thus, instead of being implemented programatically, they can be expressed declaratively through metadata such as Java annotations or XML. The Seam REST module supports both ways of declarative configurations.

For each exception type, you can specify a status code and an error message of the HTTP response.

You can configure Seam REST exception mapping directly in your Java code with Java Annotations. An exception mapping rule is defined as a @ExceptionMapping annotation. Use an @ExceptionMapping.List annotation to define multiple exception mappings.


The @ExceptionMapping annotation can be applied on any Java class in the deployment. However, it is recommended to keep all exception mapping declarations in the same place, for example, in the javax.ws.rs.core.Application subclass.


As an alternative to the annotation-based configuration, you can use the Seam Config module to configure the SeamRestConfiguration class in XML.

First, add the Seam Config module to the application. If you are using maven, you can do this by specifying the following dependency:


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


Furthermore, you can use EL expressions 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 follows the Bean Validation specification and the incomming 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.


Seam REST allows to create HTTP responses based on the defined templates. Instead of being bound to a particlar templating engine, Seam REST comes with a 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 is possible for a method to offer multiple representations of a resource, each rendered with a different template. In the example below, 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 mostly 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 built-in 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.

Seam Catch can be used for handling Java exceptions. For more information on using Seam Catch with Seam REST, refer to Section 2.1, “Seam Catch Integration”


<dependency>
    <groupId>org.jboss.seam.catch</groupId>
    <artifactId>seam-catch-api</artifactId>
    <version>${seam.catch.version}</version>
</dependency>
<dependency>
    <groupId>org.jboss.seam.catch</groupId>
    <artifactId>seam-catch-impl</artifactId>
    <version>${seam.catch.version}</version>
</dependency>

Seam Config can be used to configure Seam REST using XML. For more information on using Seam Config with Seam REST, refer to Section 2.2.2, “XML configuration”


<dependency>
    <groupId>org.jboss.seam.config</groupId>
    <artifactId>seam-config-xml</artifactId>
    <version>${seam.config.version}</version>
</dependency>

FreeMarker can be used for rendering HTTP responses. For more information on using FreeMarker with Seam REST, refer to Section 4.2.1, “FreeMarker”


<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>${freemarker.version}</version>
</dependency>

Apache Velocity can be used for rendering HTTP responses. For more information on using Velocity with Seam REST, refer to Section 4.2.2, “Apache Velocity”


<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>${velocity.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>${velocity.tools.version}</version>
</dependency>

RESTEasy Client Framework can be used for building clients of RESTful web services. For more information on using RESTEasy Client Framework, refer to Chapter 5, RESTEasy Client Framework Integration


<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>${resteasy.version}</version>
</dependency>

Note

Note that RESTEasy is provided on JBoss Application Server 6 and thus you do not need to bundle it with the web application.