大家好,我是瓜哥,本人虽然接触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);
}
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<String> friendList;
public List<String> getFriendList() {
return friendList;
}
public void setFriendList(List<String> 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<String, Object> map) {
User user = new User();
String id = (String) map.get("id");
String name = (String) map.get("name");
Integer age = (Integer) map.get("age");
List<String> hobby = (List<String>) 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<String> 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<String,String> city;
public String getName() {
return name;
}
public HashMap<String, String> getCity() {
return city;
}
public void setName(String name) {
this.name = name;
}
public void setCity(HashMap<String, String> 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<String,List<String>> city;
private List<String> nation;
public String getName() {
return name;
}
public HashMap<String, List<String>> getCity() {
return city;
}
public void setName(String name) {
this.name = name;
}
public void setCity(HashMap<String, List<String>> city) {
this.city = city;
}
public List<String> getNation() {
return nation;
}
public void setNation(List<String> nation) {
this.nation = nation;
}
}
|