【问题标题】:Jersery ExceptionMapper does not intercept Exceptions thrown from Spring AspectJ annotated Aspects?Jersey ExceptionMapper 不拦截从 Spring AspectJ 注释的方面抛出的异常?
【发布时间】:2016-05-10 05:08:22
【问题描述】:

我使用基于 Spring 注释的 AOP 来验证我对 Jersery 资源的请求。 我已经实现了一个方面,如下所示。但是这个 XBBridgeException 并没有被 Jersey ExceptionMapper 捕获,而是从 Servlet 容器中抛出。

有什么建议吗?

@Aspect
public class RequestValidationAspect {

    @Autowired
    private Validator validator;

    @Pointcut("execution(public * com.mycompany.facade.*.convertRequest(com.mycompany.BaseRequest+,..)) && args(req,..)")
    public void convertRequest(BaseRequest req) {

    }

    @Before("convertRequest(req)")
    public <T extends BaseRequest> void validateRequest(T req) throws XBBridgeException {

    Set<ConstraintViolation<T>> violations = null;
    try {
        violations = validator.validate(req, req.getValidatorGroup());
        if (!violations.isEmpty()) {
        throw new ValidationException("{validation exception}");
        }
    } catch (Throwable t) {
        t.printStackTrace();
        List<String> msgList = new ArrayList<>();
        String msg;
        if (t instanceof ValidationException) {
        msg = "{bad request}";
        for (ConstraintViolation<T> violation : violations) {
            msgList.add(violation.getPropertyPath().toString() + " " + violation.getMessage());
        }
        } else {
        msg = "{request:" + req.toString() + "}";
        msgList.add("Runtime error");
        }
        throw new XBBridgeException("request validation exception", new RequestValidationException(msg, msgList, t));
    }
    }

    public Validator getValidator() {
    return validator;
    }

    public void setValidator(Validator validator) {
    this.validator = validator;
    }

}

【问题讨论】:

    标签: spring jersey aop aspectj spring-aop


    【解决方案1】:

    好的,我找到了解决方案:

    问题是我的 ExceptionMapper 被键入以映射 XBBridgeExceptions ( ExceptionMapper )。

    但是 Spring AOP 方面 (RequestValidationAspect) 会抛出 UndeclaredThrowableException (这是一个围绕 XBBridgeException 的包装器)。 由于 UndeclaredThrowableException 是一个 RuntimeExpcetion 它没有被我的映射器拦截。解决方案是为 UndeclaredThrowableException 异常编写另一个 ExceptionMapper。

    【讨论】:

      【解决方案2】:

      我不确定 XBBridgeException 是否是您自己的异常,似乎找不到任何其他对它的引用,但如果是的话,让它扩展 RuntimeException 可能更容易。这将防止它被包装在 UndeclaredThrowableException 中,因此您可以更自然地创建您想要创建的 ExceptionMapper。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-04
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 2022-08-04
        相关资源
        最近更新 更多