zoukankan      html  css  js  c++  java
  • Springboot 5.Springboot 返回cookies信息的post接口开发

    首先创建一个类,类里面首先登陆获取到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的信息:

  • 相关阅读:
    桂林印象
    快变
    近期的事
    *C#中使用ref和out一点认识!*
    *在框架集页面放置TreeView控件时页面跳转的问题解决*
    *无法找到脚本库的问题*
    *Ajax.Net快速入门*
    *网页过期*
    *Prototype开发笔记*
    *正则表达式*
  • 原文地址:https://www.cnblogs.com/peiminer/p/9700192.html
Copyright © 2011-2022 走看看