2016-08-11 2 views
0

In einer Ruhe Anwendung, erstelle ich eine Klasse FehlerVerschachtelte Ausnahme ist nicht richtig in der ControllerAdvice berichtet

@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()); 
    } 

}

In meiner Dienstschicht zu verwalten

@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 
    } 
} 

wenn ich a ProcessPaymentException, sehe ich im Debug-Modus, wenn ich in die RestResponseEntityExceptionHandler gehe, passiere ich durch defaultExceptionHandler. Ich verstehe nicht warum, ich dachte an handleProcessPaymentException Methode zu übergeben.

Die Debug-Meldung i sehen ist:

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 

Ich dachte zu bekommen: error process payment

Antwort

0

Nach this Artikel, wenn Sie benutzerdefinierte Resolver verwendet haben, müssen Sie verwenden folgenden Zeilen definieren @ AusnahmeHandler.

@Component 
public class AnnotatedExceptionResolver extends AnnotationMethodHandlerExceptionResolver 
{ 
    public AnnotatedExceptionResolver() { 
     setOrder(HIGHEST_PRECEDENCE); 
    } 
} 
Verwandte Themen