I have a EJB project with the following modules:
- a rest module which handles the api calls
- an api module which provides an interface for the core
- a core module which contains the actual business logic
In case something goes wrong and the client should receive an error, I am using custom Exceptions, which are subclasses of WebApplicationException and already contain the appropriate response.
@ApplicationException(rollback = true)
public class InvalidParameterException extends WebApplicationException { }
These Exceptions should never be catched. When annotating it as ApplicationException, it does not get picked up by the glassfish server log. So far everything works fine.
Now I have a situation, where the core module includes another EJB module, which includes its own subclasses of WebApplicationExceptions, which look similar to the above mentioned. This Exception gets passed to the core as it should. However, when this Exception is passed to the rest module, it gets wrapped into a javax.ejb.EJBTransactionRolledbackException, which causes the glassfish server log to log all these exceptions, which I would like to avoid.
So, the @ApplicationException annotation only works when the exception is passed from the ejb module to the core module, however as soon as it is further passed to the rest module, the annotation no longer takes effect.
Is there a good way to pass the Exception as a ApplicationException through multiple modules? I know I could catch the Exception and rethrow it as an ApplicationException again, however that will bloat up the code a lot and does not seem like a very good solution. Is there a better way?
Edit: Some sample code
Rest module:
@Path("/someservice")
@Stateless
public class SomeResource {
@EJB private SomeService someService;
@GET
@Produces("application/json")
public Response someGetMethod() {
this.someService.someMethod();
return Response.ok().build();
}
}
api module:
public Interface SomeService {
public void someMethod();
}
core module:
@Stateless
public Class SomeServiceImpl implements SomeService {
@EJB private ExternalEJB externalEJB;
public void someMethod() {
externalEJB.externalMethod();
}
}
An external EJB module. Included in pom.xml (core module) via <type>ejb</type>
@Stateless
public class ExternalEJB {
public void externalMethod() {
throw new ExternalApplicationException();
}
}
Exception from external module:
@ApplicationException
public class ExternalApplicationException extends WebApplicationException {
private static final long serialVersionUID = 1L;
public ExternalApplicationException() {
super(Response.serverError().build());
}
}
@ApplicationExceptions and they are correctly bubbled up through multiple EJB invocations as excepted. However, I see your annocation hasrollback = true-- isn't therefore aEJBTransactionRolledBackExceptionexactly what you wanted? - Alexander Langerrollback = true(default is false), the behavior does not change a bit. - st-h