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 getlist2(
            @RequestParam (value = "start",required = false)int start,
            @RequestParam (value = "end",required =false )int end){

        Mapmap=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 getlist(
            @PathVariable(value = "start",required = false)int start,
            @PathVariable (value = "end",required =false )int end){

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

 

你可能感兴趣的:(Springboot)