还是拿来上节讲的代码:
package com.course.server; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; import java.util.Objects; @RestController //被告诉我是你需要扫描的类 public class MyGetMethod { @RequestMapping(value = "/getCookies",method = RequestMethod.GET) //访问的路径是什么 public String getCookies(HttpServletResponse response){ //HttpServerletRequest 装请求信息 //HttpServerletResponse 装响应信息 Cookie cookie = new Cookie("login","ture"); response.addCookie(cookie); return "恭喜你获得cookies信息成功"; } @RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET) 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.getName().equals("true")){ return "恭喜你访问成功"; } } return "你必须携带cookies信息来"; } /** * 开发一个需要携带参数才能访问的get请求 * 第一种实现方式是 url: ip:port/get/with/param?key=value&key=value * 模拟获取商品列表 开始页数,结束的页数,一页20条数据 * */ //第一种需要携带参数访问的get请求,将参数定义在方法传参位置处,用@RequestParam关键字,在浏览器地址栏中传入 @RequestMapping(value = "/get/with/param",method = RequestMethod.GET) //请求的url public Map<String,Integer> getList(@RequestParam Integer start, @RequestParam Integer end){ Map<String,Integer> myList = new HashMap<>(); myList.put("鞋",400); myList.put("衬衫",300); myList.put("干脆面",1); return myList; } /** *第2种需要携带参数访问的get请求,用到的是@PathVariable 关键字,因为是穿的路径 * url: ip:port/get/with/param/10/20 * */ @RequestMapping(value = "/get/with/param/{start}/{end}") //另一种请求url public Map myGetList(@PathVariable Integer start, @PathVariable Integer end){ Map<String,Integer> myList = new HashMap<>(); myList.put("鞋",400); myList.put("衬衫",300); myList.put("干脆面",1); return myList; } }
访问的两种方式:
原文地址:https://www.cnblogs.com/peiminer/p/9675947.html
时间: 2024-11-09 10:34:14