ExceptionMapper的执行顺序

Execution order of ExceptionMapper

我有一个异常映射器如下

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);
    }
}

所以 NotFoundException 是一个 RuntimeException。我想要 3 个异常映射器,映射

  1. 具有更高优先级的 NotFoundException
  2. 下一个优先级的 RuntimeException
  3. 终于例外

有什么办法可以优先考虑那些?

它已经以该优先级运行。最具体的命中了

来自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.

如果我没有正确理解你的问题,而是你希望所有三个映射器都被击中,那将不会发生。每个请求只命中一个映射器。这是一种避免无限循环的安全机制。