我使用基于 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;
}
}