首先創建一個類,類里面首先登陸獲取到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的信息:

