@RestControllerAdvice
public class RestControllerExceptionTranslator {
private static final Logger log = LoggerFactory.getLogger(RestControllerExceptionTranslator.class);
public RestControllerExceptionTranslator() {
}
@ExceptionHandler({AccessDenyException.class}) //统一处理AccessDenyException异常
@ResponseStatus(HttpStatus.FORBIDDEN) //返回http的状态
public CommonResult<Object> handleException(AccessDenyException exception) {
log.error(exception.getMessage());
return CommonResult.forbidden((Object)null);
}
@ExceptionHandler({UnAuthorizedException.class})
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public CommonResult<Object> handleException(UnAuthorizedException exception) {
log.error(exception.getMessage());
return CommonResult.unauthorized((Object)null);
}
@ExceptionHandler({BusinessException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<Object> handleException(BusinessException exception) {
log.error(exception.getMessage());
return CommonResult.failed(exception.getMessage());
}
}