自定义异常类
@Getter
public class ServiceException extends RuntimeException{
private final Integer code;
public ServiceException(Integer code, String msg) {
super(msg);
this.code = code;
}
}
全局异常护理类
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 异常处理方法
*/
@ExceptionHandler(ServiceException.class)
public Result<String> returnException(ServiceException e) {
return Result.error(e.getCode(), e.getMessage());
}
}
使用,在需要的地方直接
throw new ServiceException(400, "未查询到用户信息, 请重新登录");