1. 程式人生 > >開發返回cookies資訊的post介面

開發返回cookies資訊的post介面

開發返回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
在這裡插入圖片描述