我在 Spring Boot 项目中使用 @ControllerAdvice 来处理 Stripe 的卡异常,但是每次抛出此异常时,处理异常的代码都会起作用,而不是处理特定卡异常的代码。
这就是我正在做的:
“这是引发异常的 Api - 输入错误卡详细信息时出现 CardException”
@PostMapping("/charge")
public String chargePayment(HttpServletRequest request) throws CardException, AuthenticationException, InvalidRequestException, StripeException {
.......
}
“这是我的 Exeption 处理类代码”
@ControllerAdvice
public class StripeExceptionHandler {
// I want this to work when the above method/Api throws CardException.
@ExceptionHandler(CardException.class)
public String handleCardException(CardException cardException, Model model) {
model.addAttribute("exception","CardException : "+ cardException.toString());
model.addAttribute("redirectUrl", "contribute");
return "exceptionpage";
}
// But every time CardException is thrown this code works instead of the above specific code.
@ExceptionHandler(Exception.class)
public String handleGlobalExceptions(Exception exception, WebRequest webRequest, Model model) {
model.addAttribute("exception", exception.getMessage());
model.addAttribute("redirectUrl", webRequest.getDescription(false));
return "exceptionpage";
}
}