在休息应用程序中,我创建了一个用于管理错误的类
<!-- #include virtual="/path/to/includes/filename.inc" -->
}
在我的服务层
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(ProcessPaymentException.class)
private ResponseEntity < String > handleProcessPaymentException(ProcessPaymentException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
@ExceptionHandler(Exception.class)
private ResponseEntity < String > defaultExceptionHandler(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
当我得到@Transactional
@Override
public void processPayment(Long paymentId, PaymentModeEnum paymentMode) throws ProcessPaymentException {
processCreditCardPayment(paymentId, paymentMode);
}
private void processCreditCardPayment(Long paymentId, PaymentModeEnum paymentMode) throws ProcessPaymentException {
try{
chargePayment(paymentId)
}catch(ProcessPaymentException ppe){
throw new ProcessPaymentException(ppe.getMessage()); #1
}
}
private ResolverReceipt chargeMemberCreditCard(Long paymentId, PaymentGatewayConfig paymentGateway) throws ProcessPaymentException {
...
if(memberId==null){
throw new ProcessPaymentException("error process payment");#2
}
}
时,我在调试模式下看到,当我进入ProcessPaymentException
时,我经过RestResponseEntityExceptionHandler
。
我不明白为什么,我想通过defaultExceptionHandler
方法。
我看到的调试信息是:
handleProcessPaymentException
我想要得到:Target object must not be null; nested exception is java.lang.IllegalArgumentException: Target object must not be null.
e= (org.springframework.dao.InvalidDataAccessApiUsageException) org.springframework.dao.InvalidDataAccessApiUsageException: Target object must not be null; nested exception is java.lang.IllegalArgumentException: Target object must not be null
答案 0 :(得分:0)
根据this文章,如果您使用了自定义解析程序,则需要定义以下行以使用@ExceptionHandler。
@Component
public class AnnotatedExceptionResolver extends AnnotationMethodHandlerExceptionResolver
{
public AnnotatedExceptionResolver() {
setOrder(HIGHEST_PRECEDENCE);
}
}