2016-08-11 71 views
0

在其他應用程序正確地報告,我創建了一個類來管理錯誤嵌套異常沒有在ControllerAdvice

@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 

我想獲得:error process payment

回答

0

this的文章,如果您已經使用自定義解析,您需要定義以下行使用@的ExceptionHandler。

@Component 
public class AnnotatedExceptionResolver extends AnnotationMethodHandlerExceptionResolver 
{ 
    public AnnotatedExceptionResolver() { 
     setOrder(HIGHEST_PRECEDENCE); 
    } 
} 
相關問題