package cn.edu.hactcm.common; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.sql.SQLIntegrityConstraintViolationException; @RestControllerAdvice(annotations = {Controller.class, RestController.class}) @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(SQLIntegrityConstraintViolationException.class) public R ExceptionHandler(SQLIntegrityConstraintViolationException exception){ if (exception.getMessage().contains("Duplicate entry")) { String[] split = exception.getMessage().split(" "); return R.error(split[2] + "已存在"); } return R.error("用户已存在"); } @ExceptionHandler(CustomException.class) public R ExceptionHandler(CustomException exception){ return R.error(exception.getMessage()); } }