【问题标题】:Jersey Exception Mapper not handling Exceptions in Resource ConstructorJersey Exception Mapper 未处理资源构造函数中的异常
【发布时间】:2018-03-02 15:05:30
【问题描述】:

我的球衣服务器有以下异常映射器:

@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); 手动注册此提供程序

异常映射器对我的 REST 资源类中抛出的异常非常有效,但构造函数中的异常除外。

例如。

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

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

不会调用异常映射器。我该如何处理这种异常情况?

【问题讨论】:

  • 为什么还要在构造函数中产生副作用?注入的资源应在注入前进行验证。而且这里的构造函数没有参数,那么用例是什么?
  • 我在升级 Jersey 时遇到了这个问题。它在 1.19 中工作,但在 2.x 中失败。为什么一个有效的案例现在变得无效了?
  • 为此创建了泽西岛问题:github.com/eclipse-ee4j/jersey/issues/4436
  • 也许ExceptionMapper&lt;MultiException&gt;可以帮忙:stackoverflow.com/questions/28858450/…

标签: java jersey


【解决方案1】:

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

【讨论】:

  • 很遗憾没有。我已经尝试过“异常”和“运行时异常”。两者都在“正常”方法中工作,但不在构造函数中。
  • 查看我的编辑。 Beans(在 Spring 中)默认为单例,并在您注册异常映射器之前实例化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多