【发布时间】:2018-02-15 21:07:41
【问题描述】:
我的自定义响应实体如下:
@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);
}
}
我的异常类定义如下:
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 类的两个处理程序方法中添加了一条日志语句,并注意到只调用了 Generic Exception-
com.eas.common.exception.framework.CustomizedResponseEntityExceptionHandler
- CustomizedResponseEntityExceptionHandler-Generic Exception caught !!
Exception occurred during execution on the exchange:
任何建议这里缺少什么?
【问题讨论】:
标签: spring spring-boot exception-handling spring-rest