利用POSTMAN对SPringMVC程序进行API测试

本文介绍了如何利用POSTMAN对接口进行测试,首先获取API地址,结合程序配置的端口和根路径。然后确定请求方式,如未明确指定,POSTMAN支持POST和GET。接着设置请求参数,以User对象为例,确保形参匹配。通过POSTMAN发送请求,如果返回结果正确,说明接口测试成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.获取API:

进入代码的Controller层,拼接equestMapping对应的地址组成相应的API;其中端口和根目录有时候在配置文件里面已经设定:


例如:本程序在application.propertities中配置了端口为8099;根路径为/api;


package com.redstar.sample.controller;

import com.redstar.sample.vo.AuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.redstar.sample.basic.Result;
import com.redstar.sample.dto.User;
import com.redstar.sample.service.UserService;
import com.redstar.sample.vo.UserServiceVo;

import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

@Slf4j // 简单日志门面(Simple Logging Facade for Java),这里引入了lombok这个JAR包
@RestController // 定义controller类,返回的是数据类型,Controller返回的是web页面
@RequestMapping("user") // RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。
						// 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径
public class SampleController {

	@Autowired // @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
	UserService userService;


	/**
	 * 用户登录(mock)
	 * 
	 * @param loginAccount
	 * @return
	 */
	@RequestMapping("login")
	public Result login(@RequestBody User loginAccount) {
		System.out.println("login:" + loginAccount != null ? (loginAccount.getUserName() + "@@" + loginAccount.getPassword()):"");
		Result result =new Result();
		//StringUtils.isEmpty()判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 
		if (StringUtils.isEmpty(loginAccount.getUserName()) || StringUtils.isEmpty(loginAccount.getPassword())) {
			result.setCode("500");
			result.setMessage("账号或密码不允许为空");
			return result;
		}

		if (loginAccount.getUserName().equals("1") && loginAccount.getPassword().equals("123")) {

			loginAccount.setUserId(UUID.randomUUID().toString());
			result.setDataMap(loginAccount);
			result.setCode("200");
			result.setMessage("登录成功");
		} else {
			result.setCode("500");
			result.setMessage("账号或密码无效");

		}

		return result;
	}


那么下面这个程序根据端口、根路径和@RequestMapping的路径组成的HTTP路径为localhost:8099/api/user/login

2.获得请求方式

因为@RequestMapping没有像这样@RequestMapping(value = "addUser", method = { RequestMethod.POST })指定请求方式是POST还是GET,这里就是两者皆可。

3.获得对应请求参数

在POSTMAN中找到对应形参的格式。本例中形参loginAccount是个User对象,找到User对象定义的FIeld。

public @Data class User implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String userName;
	
	private String userId;
	
	private String text;

	private String password;

	private String phone;


}
这里User类内有5个Filed,写入POSTMAN的BODY内,点击Send根据返回结果,即可判断该API测试是否通过。

本例中有该用户,对应返回结果正确,接口测试成功。


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值