1.环境准备
在源代码包 java文件下面新建包 com.course.server,包下面新建 MyPostMethod.java文件。
方法体:
@RestController
@Api(
value = "/",
description = "这是我去全部的post请求"
)
@RequestMapping({"/v1"})
public class MyPostMethod {
private static Cookie cookie;
public MyPostMethod() {
}
@RequestMapping(
value = {"login"},
method = {RequestMethod.POST}
)
@ApiOperation(
value = "登录成功,成功获取cookie信息",
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 "恭喜你登录成功";
} else {
return "用户名或密码错误";
}
}
}
运行程序,调用 swagger测试页面:
http://localhost:8888/swagger-ui.html#
得到如下:
利用测试页面可以测试post接口。
右键检查,可以看到我们携带的cookies信息:
cookies信息.png
网友评论