IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> postman接口测试传参总结 -> 正文阅读

[开发测试]postman接口测试传参总结

大家好,我是瓜哥,本人虽然接触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<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;
	}
	
}

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-09-22 14:58:32  更:2021-09-22 14:59:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 20:40:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码