Mapping errors

As we have seen in the last examples, JAX-RS provides the ability to map exceptions to custom responses. This is a helpful functionality to implement transparent custom error handling without impacting the production code workflow.

A common issue when dealing with EJBs is that any thrown exception will be wrapped in an EJBException when accessed by any non-EJB context; for example, a request scoped JAX-RS resource. This makes exception handling quite cumbersome, as the EJBException would have to be unwrapped to inspect the cause.

By annotating custom exception types with @ApplicationException, the cause will not be wrapped:

import javax.ejb.ApplicationException;

@ApplicationException
public class GreetingException extends RuntimeException {

    public GreetingException(String message) {
        super(message);
    }
}

Calling an EJB that throws the GreetingException will not result in a wrapped EJBException and produce the exception type directly. The application can then define a JAX-RS exception mapper for the actual GreetingException type, similar to the one mapping constraint violations.

Specifying @ApplicationException(rollback = true) will furthermore cause the container to roll back an active transaction when the exception occurs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66