SeamFramework.orgCommunity Documentation

Chapter 2. Declarative Exception Mapping

2.1. Programmatic configuration
2.2. XML configuration
2.3. Exception Mapping

The JAX-RS specification comes with Exception Mapping Providers as a standard mechanism for treating Java exceptions. This approach works fine for complex cases, however, the exception handling logic is often trivial and not worth implementing Exception Mapper Providers for each exception type. In these situations, declarative approach is more appropriate. 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.

Seam REST exception mapping can be configured from Java code. Firstly, create an ExceptionMappingConfiguration subclass which @Specializes the provided one. Then, implement a @PostConstruct-annotated method in which the ExceptionMapping definitions are added as shown in the following example.


When the NoResultException is thrown at runtime, the client receives the following HTTP response.


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.


When an exception occurs at runtime, the SeamExceptionMapper first looks for a matching ExceptionMapping. If it finds one, it creates an HTTP response with the specified status code and error message.

The error message is marshalled within a JAXB object. As a result, the error message is available in multiple media formats. The most commonly used formats are XML and JSON. Most JAX-RS implementations provide media providers for both of these formats. In addition, the error message is also available in plain text.