本文主要是介绍Springboot框架开发微服务Mock平台之集合框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景:采用springboot相关技术,开发mock平台以备联调使用。基本类就是几个请求/响应参数及返回结果;mock就是模拟接口测试,有问题的就是根据不同的场景获取不同的响应参数满足开发或测试工作。下面先看几行代码:
@RequestMapping(value = "/Order.loanCalculate", method = RequestMethod.GET)public Result loanCalculate() {Map<String, Object> response = new LinkedHashMap<String, Object>();response.put("service_fee_desc", "还款费用明细");response.put("service_fee", "0");response.put("interest_fee", "1200");response.put("receive_amount", "18000");response.put("repay_amount", "22000");Loan repay_lan = new Loan("还款费用明细", "22000", "1");ArrayList<Loan> repay = new ArrayList<Loan>();repay.add(repay_lan);response.put("repay_lan", repay);return new Result(1, "success", response);}
解释下:返回类型Result是自己定义的,Loan也是自己定义的,如果需要修改一些返回参数,那么改动至少两个类。通常情况下,响应的参数是以json格式展现。那么思考一下,如何在同一个类中修改响应的结果的参数呢?这个时候集合类是不是可以派上用场,是否感觉高大上还可以练习?
@RequestMapping(value = "/Order.loanCalculatea", method = RequestMethod.GET)public Map<String, Object> loanCalculatea() {Map<String, Object> response = new LinkedHashMap<String, Object>();response.put("status", 1);response.put("message", "success");Map<String, Object> responselist = new LinkedHashMap<String, Object>();responselist.put("service_fee_desc", "还款费用明细");responselist.put("service_fee", "0");responselist.put("interest_fee", "1200");responselist.put("receive_amount", "18000");responselist.put("repay_amount", "22000");// Loan repay_lan = new Loan("还款费用明细", "22000", "1");
//
// ArrayList<Loan> repay = new ArrayList<Loan>();Map<String, Object> repay_lan = new LinkedHashMap<String, Object>();repay_lan.put("repay_amount_desc","还款费用明细");repay_lan.put("repay_amount", "1800");repay_lan.put("period_no", "2");responselist.putIfAbsent("repay_lan", repay_lan);response.put("repay_plan", responselist);return response;}
为了展示效果,把repay_lan的List变成了Map,实际上面就不需要关其他类什么事了,只要修改当前响应参数即可。
{"status": 1,"message": "success","repay_plan": {"service_fee_desc": "还款费用明细","service_fee": "0","interest_fee": "1200","receive_amount": "18000","repay_amount": "22000","repay_lan": {"repay_amount_desc": "还款费用明细","repay_amount": "1800","period_no": "2"}}
}
Mock平台的开发,需要一定的代码基础,要理解开发的框架,最重要的是知道写mock的意义。欢迎交流指正。
这篇关于Springboot框架开发微服务Mock平台之集合框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!