Search code examples
javajax-rsmagnolia

How to provide custom exception mappings for jax-rs endpoints in Magnolia CMS (v 5.5.6)?


I created custom jax-rs based endpoint.

@Path("/test")
public class MyEndpoint<D extends EndpointDefinition>  extends AbstractEndpoint<D> {

    @Path("/dosth")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public void doSth() {
        //some code here
    }
}

Now I want to add custom exception handling for jax-rs endpoints. I want all exceptions handling in one place, instead of try-catch in every method. Tried to add jax-rs ExceptionMapper:

@Provider
public class CustomExceptionMapper implements ExceptionMapper<Throwable> {

   public Response toResponse(Throwable e) {
      return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
   }
}

Unfortunately Magnolia doesn't use it. How can I register such mapper i Magnolia 5.5.6?


Solution

  • Custom exception mapper should be registered in rest-integration JCR configuration. The node responsible for jax-rs providers is

    config.modules.rest-integration.config.additionalProviders
    

    I've overridden default exception mapper by my custom implementation and now it works.

    enter image description here