【学习笔记】使用Springboot开发post接口

  1. 类似使用springboot开发get接口, 创建post接口如下:
package com.gracie.server;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

@RestController
@Api(value="/", description = "这是我全部的post请求")
@RequestMapping("/V1") //此处的注释的作用是:需要在后续的所有的方法前加/V1
public class MyPostMethod {
    //此变量用来装我们的cookie信息
    private static Cookie cookie;

    //用户登录成功获取到cookie信息,再访问其他接口获取到列表
    @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("gracie")&& password.equals("123456")){

            cookie=new Cookie("login", "success");
            response.addCookie(cookie);
            return "恭喜你登录成功";
        }
        return "用户名或者密码错误";


    }


}

  1. 重新运行application.run()方法:
    在这里插入图片描述3. 使用之前的swaggerUI来生成接口文档:
    在这里插入图片描述4. 测试post接口:
    在这里插入图片描述在这里插入图片描述在这里插入图片描述5. 插件介绍:lombok (参考:https://www.cnblogs.com/heyonggang/p/8638374.html)

说明: Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. (Project Lombok是一个java库,它可以自动插入编辑器和构建工具,为java增色。)
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.(永远不要再编写另一个getter或equals方法,使用一个注释,您的类就有了一个功能齐全的构建器、自动记录变量等等。)

file->setting->plugin->lombok->install在这里插入图片描述
6. 创建一个java bean:(写好了注解,get,set的方法都可以不用写了)
在这里插入图片描述注意, pom文件中需要加lombok
在这里插入图片描述
7. 验证cookie并返回用户列表:

package com.gracie.server;


import com.gracie.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") //此处的注释的作用是:需要在后续的所有的方法前加/V1
public class MyPostMethod {
    //此变量用来装我们的cookie信息
    private static Cookie cookie;

    //用户登录成功获取到cookie信息,再访问其他接口获取到列表
    @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("gracie")&& password.equals("123456")){

            cookie=new Cookie("login", "success2");
            response.addCookie(cookie);
            return "恭喜你登录成功";
        }
        return "用户名或者密码错误";
    }

    @RequestMapping(value="/get/user/list", method=RequestMethod.POST)
    @ApiOperation(value="获取用户列表", httpMethod = "POST")
    public String getUserList(HttpServletRequest request,
                            @RequestBody User u){
        User user;
        //获取所有的cookies
        Cookie[] cookies=request.getCookies();

        //验证cookies信息是否正确, u是User的一个实例,可以调用其set,get方法,是bean里的@Data注解的一个作用
        for (Cookie c: cookies){
            if (c.getName().equals("login")
                    && c.getValue().equals("success2")
                    && u.getUSERNAME().equals("toly")
                    && u.getPASSWORD().equals( "123456")){
                user=new User();
                user.setName("TuTu");
                user.setAge("3");
                user.setSex("boy");
                return user.toString();
            }
        }
        return "输入参数不正确~";


    }


}

  1. 通过swaggerUI来验证结果:

输入参数错误时:
在这里插入图片描述在这里插入图片描述输入参数正确时:
在这里插入图片描述在这里插入图片描述9. 此处无法验证cookies相关信息, 我们通过jemter来验证:
在这里插入图片描述PS: 添加debug sampler的目的是为了验证login接口中是否有cookie了:
step1: 添加一个空的cookie管理器
step2:将jmeter properties中的CookieManager.save.cookies=true(jmeter接受到的cookie会被自动存储在线程变量中,但是从Jmeter2.3.2版本后,默认不再存储,如果你想要manager自动存储收集到的cookie,你需要修改JMeter.property :CookieManager.save.cookies=true)
在这里插入图片描述PS: debug sampler的相关解释,有参考: https://blog.csdn.net/java2013liu/article/details/82984369

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值