使用注解实现异常错误
@ControllerAdvice 处理整个web controller的异常
@Slf4j @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler({ArithmeticException.class,NullPointerException.class}) //处理异常 数组可以包含多个 public String handleArithException(Exception e){ log.error("异常是:{}",e); return "login"; //视图地址 } }
@ResponseStatus
@ResponseStatus(value= HttpStatus.FORBIDDEN,reason = "用户数量太多") 定义状态信息 这个就是一个403的错误
@ResponseStatus(value= HttpStatus.FORBIDDEN,reason = "用户数量太多") public class UserTooManyException extends RuntimeException { public UserTooManyException(){ } public UserTooManyException(String message){ super(message); } }