为什么ResponseEntityExceptionHandler只捕获Generic Exception而不是特定的运行时间

时间:2018-02-15 21:07:41

标签: spring spring-boot exception-handling spring-rest

我的自定义响应实体如下:

@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(Exception.class)
@ResponseBody
public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
logger.debug ("CustomizedResponseEntityExceptionHandler-Generic Exception caught !!  "+ex.getMessage ());
    EASExceptionNotifier exceptionResponse = new EASExceptionNotifier
    (new Date(), ex.getMessage(),request.getDescription(false));
    return new ResponseEntity(exceptionResponse,HttpStatus.INTERNAL_SERVER_ERROR);
}



@ExceptionHandler(InvalidRequestTypeException.class)
public final ResponseEntity<Object> handleInvalidRequestTypeException(InvalidRequestTypeException ex, WebRequest request) {
    EASExceptionNotifier exceptionResponse = new EASExceptionNotifier
    (new Date(), ex.getMessage(),request.getDescription(false));
    return new ResponseEntity(exceptionResponse, HttpStatus.NOT_ACCEPTABLE);
}

}

My Exception类定义如下:

public class InvalidRequestTypeException extends RuntimeException {
private static Logger logger = LoggerFactory.getLogger(InvalidRequestTypeException.class);
private static final String ERR_DESC="Error desc: Invalid request type received ";

public InvalidRequestTypeException (String storeMetaData, String message) {
    super (message);
    logger.debug ("Error Message Caught >> {} ",message);
    StringBuilder finalErrorMessage= new StringBuilder();
    finalErrorMessage.append (storeMetaData).append (message);

}

客户端代码抛出错误:

if (!(requestType.equals ("RF20"))) {
throw new InvalidRequestTypeException 
 (errorData.toString()," Received wrong request type id :" + requestType);}

问题是:CustomResponseEntity类总是只捕获Generic Exception而不是InvalidRequestTypeException?

在CustomResponseEntity类的两个处理程序方法中添加了一个日志语句,并注意到只调用了通用异常 -

com.eas.common.exception.framework.CustomizedResponseEntityExceptionHandler 
- CustomizedResponseEntityExceptionHandler-Generic Exception caught !!
 Exception occurred during execution on the exchange:

有什么建议在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

由于它是Camel Processor类,我抛出InvalidRequestType Exception,最后它被转换为CamelExecutionException。

因此它总是回到Generic Exception块。