捕捉带有@RestController和@Control注解类发生的异常,@ExceptionHandler是发生这个异常后,要执行下面的方法

// 全局异常捕获
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String>  exceptionHandler(SQLIntegrityConstraintViolationException ex) {
        String msg = ex.getMessage();
        if(msg.contains("Duplicate entry")) {
            String[] split = msg.split(" ");
            String error_msg = split[2] + "已存在";
            return R.error(error_msg);
        }
        return R.error("SQL失败了");
    }
}