Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution order of ExceptionMapper

Tags:

java

jax-rs

I have an exception mapper as following

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class NotFoundMapper implements ExceptionMapper<NotFoundException> {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final MapperResponseBuilder responseBuilder = new MapperResponseBuilder();

    @Override
    public Response toResponse(NotFoundException ex) {
        log.warn("NotFoundException : " + ex.getMessage(), ex);
        return responseBuilder.buildErrorResponse(ex.getMessage(), Status.BAD_REQUEST);
    }
}

So the NotFoundException is a RuntimeException. I would like to have 3 exception mappers, which maps

  1. NotFoundException with higer priority
  2. RuntimeException with next priority
  3. finally Exception

Is there any way to priorities those ?

like image 783
dinesh707 Avatar asked Nov 26 '15 13:11

dinesh707


1 Answers

It already runs with that priority. The most specific one is hit.

From the JAX-RS spec

When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception.

If I am not understanding your question correctly, and instead you are wanting all three mappers to get hit, that won't happen. Only one mapper hit per request. It's a safety mechanism to avoid infinite loops.

like image 173
Paul Samsotha Avatar answered Oct 17 '22 08:10

Paul Samsotha