开发返回cookies信息的post接口
一、在com.course.server包MyPostMethod类下,代码如下
package com.course.server;
import com.course.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.Cookie;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
//访问地址前要加/v1
@RequestMapping("/v1")
public class MyPostMethod {
//这个变量是用来装我们cookies信息的
private static Cookie cookie;
//用户登录成功获取到cookies,如何再访问其他接口获取到列表
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ApiOperation(value = "登陆接口,成功后获取cookies信息",httpMethod = "post")
public String login(HttpServletResponse response,
@RequestParam(value = "userName",required = true) String userName,
@RequestParam(value = "password",required = true) String password){
if (userName.equals("zhangsan")&&password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登陆成功";
}
return "用户名或者密码错误!";
}
}
运行Application启动类,然后接口工具里进行调用
访问结果
二、添加Debug Sampler查看cookie信息
jmeter里添加sampler→Debug Sampler
找到就jmeter所在的bin目录下的jmeter.properties文件,将“#CookieManager.save.cookies=false”修改成“CookieManager.save.cookies=true”保存,重启jmeter
重新调用,查看Debug Sampler