import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 处理自定义异常
*
* @return Result
* @ExceptionHandler 说明捕获哪些异常,对那些异常进行处理。
*/
@ExceptionHandler(value = BusinessException.class)
public RestResponse customExceptionHandler(BusinessException exception) {
return RestResponse.setErrorResponse(exception.getErrorCode(), exception.getMessage());
}
@ExceptionHandler(value = Exception.class)
public RestResponse exceptionHandler(Exception exception) {
log.error("An exception occurred: ", exception);
return RestResponse.setErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to complete the request. Please contact the administrator.");
}
}
全局异常处理
于 2024-01-27 17:24:51 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)