返回cookies信息的post请求的开发


在包 com.zuolh.lab 下创建一个 MyPostMethod 类,代码如下:

package com.zuolh.lab;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * <p/>
 *
 * @author zuolihong
 * @version 1.0
 * @className MyPostMethod
 * @date 2021/5/8 17:22
 */
@RestController

//访问地址前要加/postApi
@RequestMapping("/postApi")
public class MyPostMethod {
    private static Cookie cookie;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录成功后获取cookies信息", httpMethod = "post")
    @ApiImplicitParam(name = "登录", required = true, paramType = "query")
    public String login(HttpServletResponse response,
                        @RequestParam(value = "loginName", required = true) String loginName,
                        @RequestParam(value = "password", required = true) String password) {
        if (loginName.equals("zuolh") && password.equals("111111")) {
            cookie = new Cookie("login", "success");
            response.addCookie(cookie);
            return "恭喜登录成功";
        }
        return "用户名或者密码错误";
    }

}

运行成功后通过 jmeter 进行调用:
image.png
调用结果:
image.png

添加 Debug Sampler 查看 cookie 信息 jmeter 里添加 sampler→Debug Sampler,去 jmeter 所在的 bin 目录下的 jmeter.properties 文件,将“#CookieManager.save.cookies=false”修改成“CookieManager.save.cookies=true”保存,重启 jmeter,重新调用,查看 Debug Sampler
image.png

image.png


文章作者: zuolh
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 zuolh !
  目录