使用注解实现异常错误
作者:互联网
@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); } }
标签:错误,class,UserTooManyException,public,注解,message,异常,ResponseStatus 来源: https://www.cnblogs.com/dzs894330350/p/16690407.html