postman接口测试传参总结

您所在的位置:网站首页 postman入参和出参左右分布 postman接口测试传参总结

postman接口测试传参总结

2024-07-15 21:40| 来源: 网络整理| 查看: 265

大家好,我是瓜哥,本人虽然接触java多年时间,但是在刚开始接触javaweb开发的时候,对于前端界面向后端传参也不是很清楚。最近刚开始入职不久的一个软件测试妹子一直在做接口测试相关的工作,由于她之前没有接触过web开发相关的工作。所以对于接口测试中各种接口不通出现的问题经常不知道如何解决。其实在接口测试当中很大一部分是由于接口接受参数不正常而造成的。对此本人将接口测试的各种传参总结如下:并且利用postman进行接口测试。

一、GET方式的接口

1、不传递参数请求:http://localhost:8082/user0

Postman执行结果

Java代码

@GetMapping("/user0") public String user0() { logger.info("开始执行selectUserFromDB方法---------------------"); User user = new User(); user.setName("小红"); user.setAge(15); user.setSex("男"); logger.info("selectUserFromDB查询用户信息为:" + JSON.toJSONString(user)); try { int a = 10; int b = 10; int c = a / b; } catch (Exception e) { e.printStackTrace(); logger.error("计算出现异常,异常信息为:", e); } return JSON.toJSONString(user); }

2、传递参数请求:http://localhost:8082/user1?username=yangdechao

postman执行结果

Java代码

@GetMapping("/user1") public String user1(@RequestParam("username") String username) { logger.info("开始执行selectUserFromDB方法---------------------"); User user = new User(); user.setName(username); user.setAge(15); user.setSex("男"); logger.info("selectUserFromDB查询用户信息为:" + JSON.toJSONString(user)); try { int a = 10; int b = 10; int c = a / b; } catch (Exception e) { e.printStackTrace(); logger.error("计算出现异常,异常信息为:", e); } return JSON.toJSONString(user); }

3、get传递多参数请求:http://localhost:8082/user2?username=yangdechao&password=123456

Postman执行结果

Java代码

@ResponseBody @GetMapping(value = "/user2") public String user2(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) { User user = new User(); user.setName(username); user.setPassword(password); return JSON.toJSONString(user); }

4、传递参数请求:http://localhost:8082/user3/yangdechao

Postman执行结果

Java代码

@GetMapping("/user3/{usename}") @ResponseBody public String user3(@PathVariable(name = "usename", required = true) String usename) { User user = new User(); user.setName(usename); return JSON.toJSONString(user); }

二、POST方式的接口

1、传递实体对象参数请求:http://localhost:8082/user5

Postman执行结果

Java代码

@PostMapping("/user5") @ResponseBody public String user4(@RequestBody User user) { return JSON.toJSONString(user); }

User实体类

package cn.com.guage.user.entity; import java.io.Serializable; import java.util.List; public class User implements Serializable{ /** * */ private static final long serialVersionUID = 1L; /** * */ private String id; private String name; private String password; private String sex; private int age; private List friendList; public List getFriendList() { return friendList; } public void setFriendList(List friendList) { this.friendList = friendList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } }

2、传递map参数请求:http://localhost:8082/user6

Postman执行结果

Java代码

@PostMapping("/user6") public User user6(@RequestBody Map map) { User user = new User(); String id = (String) map.get("id"); String name = (String) map.get("name"); Integer age = (Integer) map.get("age"); List hobby = (List) map.get("hobby"); user.setId(id); user.setAge(age); user.setName(name); user.setFriendList(hobby); return user; }

3、Postman执行结果传递List参数请求:http://localhost:8082/user7

postman执行结果

Java代码

@PostMapping("/user7") public User user7(@RequestBody List cardIdList) { User user = new User(); user.setFriendList(cardIdList); return user; }

4、传递实体参数,实体里面有map请求:http://localhost:8082/user8

Postman执行结果

Java代码

@PostMapping("/user8") public ProvinceEntity user8(@RequestBody ProvinceEntity provinceEntity) { return provinceEntity; }

ProvinceEntity实体类

package cn.com.guage.user.entity; import java.util.HashMap; public class ProvinceEntity { private String name; private HashMap city; public String getName() { return name; } public HashMap getCity() { return city; } public void setName(String name) { this.name = name; } public void setCity(HashMap city) { this.city = city; } }

5、传递实体参数,实体里面有map和list请求:http://localhost:8082/user9

Postman执行结果

java代码

@PostMapping("/user9") public ProvinceEntity2 user9(@RequestBody ProvinceEntity2 provinceEntity2) { return provinceEntity2; }

 ProvinceEntity2 实体代码

package cn.com.guage.user.entity; import java.util.HashMap; import java.util.List; public class ProvinceEntity2 { private String name; private HashMap city; private List nation; public String getName() { return name; } public HashMap getCity() { return city; } public void setName(String name) { this.name = name; } public void setCity(HashMap city) { this.city = city; } public List getNation() { return nation; } public void setNation(List nation) { this.nation = nation; } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3