cancel
Showing results for 
Search instead for 
Did you mean: 

How to customize JAX-RS application exception handling?

Steven_Huwig1
Star Contributor
Star Contributor

Hello,

I have configured a simple JAX-RS (not WebEngine as it is for programmatic web services) application as described in the documentation. I am using the

Nuxeo-WebModule: org.nuxeo.ecm.webengine.jaxrs.scan.DynamicApplicationFactory

line to discover my JAX-RS classes.

Everything is working as I need, with the small detail that the text/plain error response for WebExceptions that are raised in my JAX-RS classes includes the stack trace. I want them to only include the status line with an empty body.

What is the correct way to avoid/modify this behavior? I tried putting an ExceptionMapper implementation in my JAX-RS package but the DynamicApplicationFactory does not seem to discover/apply it. It is annotated with @Provider and is in the correct package.

Thanks.

5 REPLIES 5

Damien_Metzler
Star Contributor
Star Contributor

You can override the handleError() method in your ModuleRoot class. Here is a sample

@Override
public Object handleError(WebApplicationException e) {
    if (e instanceof WebSecurityException) {
        return Response.status(401).entity(
                getTemplate("error/error_401.ftl")).type("text/html").build();
    } else if (e instanceof WebResourceNotFoundException) {
        return Response.status(404).entity(
                getTemplate("error/error_404.ftl")).type("text/html").build();
    } else {
        return super.handleError(e);
    }
}

Hmm, I don't have a ModuleRoot class. I just have POJOs annotated with the JAX-RS @Path and @Provider, and the DynamicApplicationFactory is doing the correct thing with respect to making those available.

Ok, sorry i didn't read well your message and thought you were in a WebEngine app. BTW you can use WebEngine for a "programatic webservice"

Thanks for your help! I did look at the rest of the documentation and decided against using the WebEngine app for the time being, mostly because I really didn't need any of the .ftl or other tools for building more complex systems, since this is only a couple of JSON-driven web services for creating and polling specific documents in the repository.

Any more thoughts on this, Damien? I'm prepared to accept "there isn't a way right now" as an answer for now.

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.