springboot之带参数的get请求

1.了解get方法有2中注解,@RequestMapping和@GetMapping,

第一种:

//第一种用@RequestMapping注解指定method为get,value代表路径形参,HTTPServletResponse代表响应
 @RequestMapping(value = "/getCookies", method = RequestMethod.GET)
    public String getCookies(HttpServletResponse response) {
        Cookie cookie = new Cookie("login", "true");
        response.addCookie(cookie);
        return "geted cookies successful";
    }

第二种:

    /*

        第二种通过注解@GetMapping,HttpServletRequest代表请求
    
     */
    @GetMapping(value = "get/with/cookies")
    public String getwithcookies(
        HttpServletRequest request){
        Cookie [] cookies=request.getCookies();
        if(Objects.isNull(cookies)){
            return "你必须携带cookies请求";
        }
        for(Cookie cookie:cookies){
            if (cookie.getName().equals("login")
                    &&cookie.getValue().equals("true")){
                return "登录成功";
            }
        }
      return "cookies错误";
    }

2,在get请求里面添加参数,也有2中方式(@RequestParam和@PathVarible)

第一种:

   /**
     开发一个需要携带参数才能访问的get请求
    第一种 url:key=value&key=value
     **/
    @GetMapping(value = "/get/with/param")
    public Map<String,Integer> getlist2(
            @RequestParam (value = "start",required = false)int start,
            @RequestParam (value = "end",required =false )int end){

        Map<String,Integer>map=new HashMap<>();
        map.put("干脆面",100);
        map.put("衣服",200);
        map.put("鞋子",200);
        return map;
    }

第二种:

  /**
     * 第二种携带参数访问的请求
     * url:port/get/with/param/10/20
     */
    @GetMapping(value = "/get/with/param/{start}/{end}")
    public Map<String,Integer> getlist(
            @PathVariable(value = "start",required = false)int start,
            @PathVariable (value = "end",required =false )int end){

        Map<String,Integer>map=new HashMap<>();
        map.put("干脆面",100);
        map.put("衣服",200);
        map.put("鞋子",200);
        return map;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值