@RestControllerAdvice
public class GlobalExceptionHandler {
Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = RuntimeException.class)
public ApiResponse error(RuntimeException exception) {
logger.info("系统出现错误{}",exception.getMessage());
return ApiResponse.fail(exception.getMessage());
}
}
使用这种无法记录出错误的详细信息,比如出错的方法,行数等等,需要这样子,直接把
异常实体传入
@RestControllerAdvice
public class GlobalExceptionHandler {
Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = RuntimeException.class)
public ApiResponse error(RuntimeException exception) {
logger.info("系统出现错误",exception);
return ApiResponse.fail(exception.getMessage());
}
}