接口可维可测;统一管理

一、建立统一的Result对象(便于统一处理请求结果)
1、赋予code、msg、data属性
2、data的类型为范型
3、代码参考:

  public class Result<T> {
  		private Integer code;// 错误码
  		private String msg; // 提示信息
  		private T data; // 任意对象
		gettter()
		setter()
  }
  
  结果内容格式:
  
  {
  	code:404,
  	msg:“地址错误,无法找到方法”,
  	data:null
  }

  {
	code:404,
  	msg:“地址错误,无法找到”,
  	data:{
  		probe:{...},
  		mrf:{...}
  		}
  	}

二、错误码统一管理(统一管理,方便维护)
1、创建enums包,包下存放各枚举类
2、针对上文示例:创建错误码枚举类ResultCodeEnum
3、代码参考

 public enum ResultCodeEnum{
 		SUCCESS(0,“请求成功”),
 		SYSTEM_ERROR(-1,“系统错误”),
 		UNFIND_METHOD(404,“地址错误,无法找到方法”),
 		PARSE_ERROR(343, “数据解析异常”),
 		PARSE_TIMEOUT(443,“数据解析超时”),
 		MISS_CONFIG(302, “数据配置缺失”),
 		MISS_CELL(303,“数据小区信息缺失”),
 		;
 		private Integer code;
 		private String msg;
 		ResultCodeEnum(Integer code, String msg){
 				this.code = code;
 				this.msg = msg;
 		}
 		gettter(); // 枚举类常为引用,一般不需要setter()
 }

三、利用AOP思想,统一处理异常
1、创建exception包,包下存放自定义异常
2、创建Aspect包,包下存放AOP切面文件
3、AOP使用方式简介
3-1、pom文件引入依赖
3-2、创建HttpAspect类,加入注解@Aspect 同时需要加@Component放到容器中去
3-3、抽取切点方法,括号内的。。代表任意参数
代码参考

	@PointCut(“execution(public * com.test.rumen.demo.controller.*(..))”)
	public void cut(){}
	
	@Before(“cut()”)// 在接口执行前,定义了切点就可以不写全地址了
	public void doBefore(){
	 // 可以用作权限校验,表单验证等
	 // 可以做请求记录,请求地址,ip,参数等等
	}
	@After(“cut()”)//在接口执行后
	public void doAfter(){
	}
	@AfterReturning(returning = “object”,pointcut=“cut()”)
	public void doAfterReturning(object obj){
	// 可以做结果记录,结果处理等
	}
4、自定义的faultException 加入自己需要定义的内容
代码参考
public class FaultException extends RuntimeException{
		private Integer code;
		public FaultException (ResultCodeEnum enum){
				super(enum.getMsg());
				this.code = enum.getCode();
		}
		gettter()
		setter()
}
5、方法执行中如果有异常,可抛出自定义异常
6、新建Exceptionhandle类,处理接到的异常
代码参考
@ControllerAdvice
public class ExceptionHandle{
	
	public Result dealWithResult(Exception e){
			if(e instanceof FaultException ){
					FaultException  exception = (FaultException )e;
					return ResultUtil.error(exception.getCode(),exception.getMessage())
			}
			return ResultUtil.error(-1,"系统错误");
	}
}

public class ResultUtil{
	public static Result success(Object obj){
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setData(obj);
		return result;
	}
	
	public static Result error(Integer code, String msg){
		Result result = new Result();
		result.setCode(code);
		result.setMsg(msg);
		return result ;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了完成此任务,我们需要设计一个简单的交通工具系统,包括飞机、汽车和摩托车三种交通工具类型。每种交通工具都有自己的品牌、重量和乘客数属性,并且可以进行加速、减速和刹车等操作。 我们可以使用C++中的继承和组合来实现这个交通工具系统。具体来说,我们可以定义一个基类Vehicle,它包含品牌、重量和乘客数三个属性,以及加速、减速和刹车等方法。然后,我们可以从Vehicle派生出三个类Plane、Car和Motor,分别实现它们自己的加速、减速和刹车等方法。 此外,我们还可以定义一个Factory类,用于创建各种交通工具类型的对象。然后,在Plane、Car和Motor类中使用虚继承的方式,继承自Vehicle类,以便每种交通工具都可以拥有自己的品牌、重量和乘客数属性,并且可以进行加速、减速和刹车等操作。 下面是一个示例代码,用于实现上述功能: ``` #include <iostream> #include <string> using namespace std; class Vehicle { public: Vehicle(const string& brand, double weight, int passenger) : m_brand(brand), m_weight(weight), m_passenger(passenger) {} virtual void accelerate() = 0; virtual void decelerate() = 0; virtual void brake() = 0; virtual ~Vehicle() {} protected: string m_brand; double m_weight; int m_passenger; }; class Factory { public: static Vehicle* createVehicle(const string& type, const string& brand, double weight, int passenger); }; class Plane : virtual public Vehicle { public: Plane(const string& brand, double weight, int passenger, int wing) : Vehicle(brand, weight, passenger), m_wing(wing) {} void accelerate() override { cout << "Plane is accelerating." << endl; } void decelerate() override { cout << "Plane is decelerating." << endl; } void brake() override { cout << "Plane is braking." << endl; } protected: int m_wing; }; class Car : virtual public Vehicle { public: Car(const string& brand, double weight, int passenger, int gear) : Vehicle(brand, weight, passenger), m_gear(gear) {} void accelerate() override { cout << "Car is accelerating." << endl; } void decelerate() override { cout << "Car is decelerating." << endl; } void brake() override { cout << "Car is braking." << endl; } protected: int m_gear; }; class Motor : virtual public Vehicle { public: Motor(const string& brand, double weight, int passenger, int cylinder) : Vehicle(brand, weight, passenger), m_cylinder(cylinder) {} void accelerate() override { cout << "Motor is accelerating." << endl; } void decelerate() override { cout << "Motor is decelerating." << endl; } void brake() override { cout << "Motor is braking." << endl; } protected: int m_cylinder; }; Vehicle* Factory::createVehicle(const string& type, const string& brand, double weight, int passenger) { if (type == "plane") { return new Plane(brand, weight, passenger, 2); } else if (type == "car") { return new Car(brand, weight, passenger, 5); } else if (type == "motor") { return new Motor(brand, weight, passenger, 2); } else { return nullptr; } } int main() { Vehicle* plane = Factory::createVehicle("plane", "flying", 60.0, 12); Vehicle* car = Factory::createVehicle("car", "BYD", 1.5, 5); Vehicle* motor = Factory::createVehicle("motor", "Honda", 0.2, 2); plane->accelerate(); car->decelerate(); motor->brake(); delete plane; delete car; delete motor; return 0; } ``` 使用上述代码,我们可以创建各种交通工具对象,并进行加速、减速和刹车操作,例如: ``` Vehicle's Constructor with parameters plane's Constructor with 1 parameter Car's Constructor with 1 parameter Motor's Constructor with parameters Plane is accelerating. Car is decelerating. Motor is braking. Vehicle's Destructor plane's Destructor Vehicle's Destructor Car's Destructor Vehicle's Destructor Motor's Destructor ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值