首先创建一个类,类里面首先登陆获取到cookie,然后带着cookie去发送请求
package com.course.server; import com.course.bean.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @RestController @Api(value = "/",description = "这是我全部的post请求") @RequestMapping("v1") public class MyPostMethod { //cookie变量用来装我们的cookies信息 private static Cookie cookie; //用户登陆成功获取到cookies,然后在访问其他接口获取到的列表,必须要求是post方法才可以访问 @RequestMapping(value = "/login",method = RequestMethod.POST) @ApiOperation(value = "登陆接口,成功后获取cookies信息",httpMethod = "POST") public String login(HttpServletResponse response, //传入response这个参数是会在浏览器中看到cookie的属性 @RequestParam(value = "usenName",required = true) String userName, //true代表一定要传 @RequestParam(value = "password",required = true) String password){ if(userName.equals("zhangsan")&&password.equals("123456")){ cookie = new Cookie("login","true"); response.addCookie(cookie); return "恭喜你登陆成功!"; } return "用户名或者密码错误!"; } }
重新启动类,然后在http://localhost:8888/swagger-ui.html 点击 my-post-method,点击/v1/login,填入zhangsan 123456,点击try out
以下是cookie的信息: