网站开发计划书网站技术解决方案,怎么注册微信号,电商运营工作内容,软件开发的周期分几个阶段一、前言
在java开发中经常需要造数据进行测试接口#xff0c;这里记录一下常用的通过造数据测试接口的方法。 二、一般的接口传参方式
1、接口的方式最好是使用JSON或者map的方式#xff0c;这样的好处是传参可以灵活伸缩#xff0c;返回的结果也最好是JSON或者map的方式…一、前言
在java开发中经常需要造数据进行测试接口这里记录一下常用的通过造数据测试接口的方法。 二、一般的接口传参方式
1、接口的方式最好是使用JSON或者map的方式这样的好处是传参可以灵活伸缩返回的结果也最好是JSON或者map的方式 /*** 获取授权访问令牌accessToken* param params* return*/PostMapping(token)JSONObject getAccessToken(RequestBody MapString, Object params);
2、组装参数方法 /*** 组装获取Access Token参数*/public MapString, Object getACTokenParam() {MapString, Object map new HashMap();String timestamp DateTimeUtils.getDateStr();client_secrettimestampclient_idusernamepasswordgrant_typescopeclient_secretString clientSecM MD5Util.getMd5(clientSecret).toUpperCase();String passwordM MD5Util.getMd5(password).toUpperCase();String subString (clientSecM timestamp clientId userName passwordM grantType scope clientSecM);String sign MD5Util.getMd5(subString).toUpperCase();map.put(grant_type, grantType);map.put(client_id, clientId);map.put(timestamp, timestamp);map.put(username, userName);map.put(password, passwordM);map.put(scope, scope);map.put(sign, sign);return map;}
3.常见的造数据
随机生成4位数 Random rand new Random(); int randomNum rand.nextInt(9000) 1000;
休眠
//休眠3秒 Thread.sleep(3000);
获取guid /** * 获取guid * * return */ public static String getGuid() { return UUID.randomUUID().toString().replaceAll(\\-, ); } 。。 /*** 获取guid* * return*/public static String getGuid() {return UUID.randomUUID().toString().replaceAll(\\-, );}三、示例 /*** 测试用户* throws Exception*/public void testUser() throws Exception {// 创建一个ObjectMapper对象ObjectMapper mapper new ObjectMapper();// 创建一个Json对象ObjectNode json mapper.createObjectNode();URL url new URL(http://127.0.0.1/tourist-contract/register);for (int i 0; i 500; i) {Random rand new Random();int randomNum rand.nextInt(9000) 1000;json.put(Phone, 1308111randomNum);json.put(openID, user_20000 i);json.put(UserName, test_ i);json.put(unifyID, test_ i);json.put(userID, test_ i);json.put(reservedField, reservedField);ObjectNode json1 mapper.createObjectNode();json1.put(sender, json);json1.put(mark, travel);log.info(注册用户上链参数json:json1);//休眠3秒Thread.sleep(3000);// 接口调用ltApi(json1, url, 1308111randomNum, 用户注册, 新增);}}