学习笔记(十八)项目实战接口开发SprintBoot

博客围绕Spring Boot展开,介绍了带cookies信息访问的get接口开发,启动Spring Boot后访问特定地址,对比有无cookies信息的访问情况。还阐述了需要携带参数的get请求的两种开发方式,以及扩展的携带参数和cookie才能访问的get方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第4节 一个要求携带cookies信息访问的get接口开发

    /**
     * 这是一个需要携带cookies信息才能访问的get请求
     */
    @RequestMapping(value = "get/with/cookies", method = RequestMethod.GET)
    public String getWithCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "no cookies with get method";
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("login") && cookie.getValue().equals("true")) {
                return "这是一个需要携带cookies信息才能访问的get请求";
            }
        }
        return "cookies info wrong";
    }

启动springboot后访问“http://localhost:8080/get/with/cookies

第一次访问没有cookies信息,第二次携带cookie信息

第5节 需要携带参数的get请求的两种开发方式

方法一:


    /**
     * 开发一个需要携带参数才能访问的get请求
     * 第一种实现方式:url:key=value&key=value
     * 我们来模拟获取商品列表
     */
    @RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
    public Map<String, Integer> getList(@RequestParam Integer start,
                                        @RequestParam Integer end) {
        Map<String, Integer> myLsit = new HashMap<String, Integer>();
        myLsit.put("鞋子", 400);
        myLsit.put("方便面", 5);
        myLsit.put("口红", 300);

        return myLsit;
    }

方法二:

 /**
     * 开发一个需要携带参数才能访问的get请求
     * 第二种实现方式:url:url:port/get/with/param/10/20
     * 我们来模拟获取商品列表
     */
    @RequestMapping(value = "/get/with/param/{start}/{end}", method = RequestMethod.GET)
    public Map<String, Integer> getMyList(@PathVariable Integer start,
                                          @PathVariable Integer end) {
        Map<String, Integer> myLsit = new HashMap<String, Integer>();
        myLsit.put("鞋子", 400);
        myLsit.put("方便面", 5);
        myLsit.put("口红", 300);

        return myLsit;
    }

扩展---一个携带参数和cookie才能访问的get方法


    /**
     * 开发一个需要携带参数和cookies才能访问的get请求
     * 我们来模拟用户使用用户名和密码登录
     */
    @RequestMapping(value = "/get/with/paramAndCookie", method = RequestMethod.GET)
    public String login(HttpServletRequest request,
                        @RequestParam String name,
                        @RequestParam String password) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "请配置cookie信息";
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("login") & cookie.getValue().equals("true")) {
                if (name.equals("zhangsan") && password.equals("123456")) {
                    return "zhangsan 登录成功";
                } else if (name.equals("zhangsan") && !password.equals("123456")) {
                    return "密码错误请重试";
                } else {
                    return "请注册再登录";
                }
            }
        }
        return "请注册再登录";
    }

重启springboot后访问该接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值