Jersey Exception Mapper不处理Resource Constructor中的异常

问题描述 投票:0回答:1

我的jersey-server有以下Exception Mapper:

@Provider
public class ExceptionResponseMapper implements ExceptionMapper<Throwable> {

    Logger logger = Logger.getLogger(ExceptionResponseMapper.class);

    @Override
    public Response toResponse(Throwable e) {
        log.error(e);
        Response response = Response.status(500)
                    .entity(e)
                    .build();

        return response;
    }
}

我使用config.register(ExceptionResponseMapper.class);手动注册此提供程序

除了构造函数中的异常之外,Exception Mapper对我的REST资源类中抛出的异常非常有效。

例如。

@Path("/MyResource")
public class MyResource {

    public MyResource() {
        throw new RuntimeException();
    }
}

不会调用Exception Mapper。我该如何处理这种例外情况?

java jersey
1个回答
0
投票

当您的资源由Spring或CDI创建时,Jersey Context尚未就绪,但我的猜测是(如果您使用的是DI框架)。

© www.soinside.com 2019 - 2024. All rights reserved.