
image.png
1、模擬get請求
2、模擬get請求返回cookie
3、模擬get請求攜帶cookie信息
4、模擬get請求攜帶參數(shù)
5、模擬get請求攜帶參數(shù)二
6、模擬post請求返回cookies信息
7、模擬post請求攜帶cookies信息
1、模擬get請求
package com.course.server;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyGetMethod {
@RequestMapping(value = "/useSpringBoot/getcookies",method = RequestMethod.GET)
public String getCookie(){
return "這是我的返回結(jié)果";
}
}
2、模擬get請求返回cookie
package com.course.server;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
public class MyGetMethod {
//模擬get請求
@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)
public String get(){
return "這是我的get請求的返回結(jié)果";
}
//模擬get請求返回cookie信息
@RequestMapping(value = "/useSpringBoot/getcookies",method = RequestMethod.GET)
public String getCookie(HttpServletResponse response){
Cookie cookie=new Cookie("login","true");
response.addCookie(cookie);
return "這是我的get請求返回cookie的請求結(jié)果";
}
}
3、模擬get請求攜帶cookie信息
package com.course.server;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
@RestController
public class MyGetMethod {
//模擬get請求
@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)
public String get(){
return "這是我的get請求的返回結(jié)果";
}
//模擬get請求返回cookie信息
//HttpServletResponse類是一個存儲器,可以把cookie等信息加入到response中返回
@RequestMapping(value = "/useSpringBoot/getResponseCookies",method = RequestMethod.GET)
public String getResponseCookie(HttpServletResponse response){
Cookie cookie=new Cookie("login","true");
response.addCookie(cookie);
return "這是我的get請求返回cookie的請求結(jié)果";
}
//模擬get請求攜帶cookie信息
//HttpServletRequest類是一個存儲器,可以把cookie等信息加入到請求中去訪問get請求
@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)
public String getwithCookies(HttpServletRequest request){
//從get請求攜帶的cookie中獲取cookie,并賦值給cookies數(shù)組
Cookie[] cookies = request.getCookies();
//判斷cookies數(shù)組是否為空,如果為空返回錯誤,不為空判斷cookies是不是獲取到的cookies
if(Objects.isNull(cookies)){
return "必須攜帶cookie信息";
}
//foreach循環(huán)cookies,對象類型是cookie類,利用cookie類的getName和getValue方法判斷cookies信息
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
return "這是一個攜帶cookie信息的get請求";
}
}
return "cookies信息校驗(yàn)失敗";
}
}
4、模擬get請求攜帶參數(shù)
package com.course.server;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
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 {
//模擬get請求
@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)
public String get(){
return "這是我的get請求的返回結(jié)果";
}
//模擬get請求返回cookie信息
//HttpServletResponse類是一個存儲器,可以把cookie等信息加入到response中返回
@RequestMapping(value = "/useSpringBoot/getResponseCookies",method = RequestMethod.GET)
public String getResponseCookie(HttpServletResponse response){
Cookie cookie=new Cookie("login","true");
response.addCookie(cookie);
return "這是我的get請求返回cookie的請求結(jié)果";
}
//模擬get請求攜帶cookie信息
//HttpServletRequest類是一個存儲器,可以把cookie等信息加入到請求中去訪問get請求
@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)
public String getwithCookies(HttpServletRequest request){
//從get請求攜帶的cookie中獲取cookie,并賦值給cookies數(shù)組
Cookie[] cookies = request.getCookies();
//判斷cookies數(shù)組是否為空,如果為空返回錯誤,不為空判斷cookies是不是獲取到的cookies
if(Objects.isNull(cookies)){
return "必須攜帶cookie信息";
}
//foreach循環(huán)cookies,對象類型是cookie類,利用cookie類的getName和getValue方法判斷cookies信息
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
return "這是一個攜帶cookie信息的get請求";
}
}
return "cookies信息校驗(yàn)失敗";
}
//模擬get請求攜帶參數(shù)
@RequestMapping(value = "/useSpringBoot/getwithParam",method = RequestMethod.GET)
public Map<String,String> getwithParam(@RequestParam("goodsid") String goodsid,
@RequestParam("goodsdes") String goodsdes){
Map<String,String> map=new HashMap<>();
map.put("goodsid",goodsid);
map.put("goodsdes",goodsdes);
return map;
}
}
5、模擬get請求攜帶參數(shù)二,使用@PathVariable注解獲取參數(shù)值
package com.course.server;
import org.springframework.stereotype.Controller;
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 {
//模擬get請求
@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)
public String get(){
return "這是我的get請求的返回結(jié)果";
}
//模擬get請求返回cookie信息
//HttpServletResponse類是一個存儲器,可以把cookie等信息加入到response中返回
@RequestMapping(value = "/useSpringBoot/getResponseCookies",method = RequestMethod.GET)
public String getResponseCookie(HttpServletResponse response){
Cookie cookie=new Cookie("login","true");
response.addCookie(cookie);
return "這是我的get請求返回cookie的請求結(jié)果";
}
//模擬get請求攜帶cookie信息
//HttpServletRequest類是一個存儲器,可以把cookie等信息加入到請求中去訪問get請求
@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)
public String getwithCookies(HttpServletRequest request){
//從get請求攜帶的cookie中獲取cookie,并賦值給cookies數(shù)組
Cookie[] cookies = request.getCookies();
//判斷cookies數(shù)組是否為空,如果為空返回錯誤,不為空判斷cookies是不是獲取到的cookies
if(Objects.isNull(cookies)){
return "必須攜帶cookie信息";
}
//foreach循環(huán)cookies,對象類型是cookie類,利用cookie類的getName和getValue方法判斷cookies信息
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
return "這是一個攜帶cookie信息的get請求";
}
}
return "cookies信息校驗(yàn)失敗";
}
//模擬get請求攜帶參數(shù)
@RequestMapping(value = "/useSpringBoot/getwithParam",method = RequestMethod.GET)
public Map<String,String> getwithParam(@RequestParam("goodsid") String goodsid,
@RequestParam("goodsdes") String goodsdes){
Map<String,String> map=new HashMap<>();
map.put("goodsid",goodsid);
map.put("goodsdes",goodsdes);
return map;
}
//模擬get請求攜帶參數(shù) @PathVariable
@RequestMapping(value = "/useSpringBoot/getwithParamtwo/{goodsid}/{goodsdes}",method = RequestMethod.GET)
public Map<String,String> getwithParamtwo(@PathVariable("goodsid") String goodsid,
@PathVariable("goodsdes") String goodsdes){
Map<String,String> map=new HashMap<>();
map.put("goodsid",goodsid);
map.put("goodsdes",goodsdes);
return map;
}
}
6、模擬post請求返回cookies信息
package com.course.server;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(value = "/",description = "post請求接口")
public class MyPostMethod {
//定義cookie全局變量,供需要攜帶cookie的接口使用
Cookie cookie;
//定義一個login的post方法,然后返回cookie信息
@RequestMapping(value = "/useSpringBoot/postlogin",method = RequestMethod.POST)
@ApiOperation(value = "登錄接口返回cookie信息",httpMethod = "POST")
public String Login(HttpServletResponse response,
@RequestParam("name") String name,
@RequestParam("password") String password){
//如果用戶名和密碼成功,就添加cookie信息,然后添加到response中,然后成功信息
if(name.equals("qinzhenxia") && password.equals("123456")){
cookie=new Cookie("token","1234567890");
response.addCookie(cookie);
return "返回cookie,登錄成功";
}
return "登錄失敗";
}
}
7、模擬post請求攜帶cookies信息
以下代碼中g(shù)etusers方法就是攜帶cookies信息的post請求
package com.course.server;
import com.course.Bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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請求接口")
public class MyPostMethod {
//定義cookie全局變量,供需要攜帶cookie的接口使用
Cookie cookie;
//定義一個login的post方法,然后返回cookie信息
@RequestMapping(value = "/useSpringBoot/postlogin", method = RequestMethod.POST)
@ApiOperation(value = "登錄接口返回cookie信息", httpMethod = "POST")
public String Login(HttpServletResponse response,
@RequestParam("name") String name,
@RequestParam("password") String password) {
//如果用戶名和密碼成功,就添加cookie信息,然后添加到response中,然后成功信息
if (name.equals("qinzhenxia") && password.equals("123456")) {
cookie = new Cookie("token", "1234567890");
//在response中添加cookie并返回cookie
response.addCookie(cookie);
return "返回cookie,登錄成功";
}
return "success";
}
//創(chuàng)建users對象,getusers接口會用到該對象創(chuàng)建用戶登錄信息。
User users;
//用postman請求Login接口,會自動存儲cookie,再請求getusers接口時會自動把cookie帶上,所以請求時不需要再單獨(dú)處理cookie
//攜帶cookie信息請求接口,同時返回用戶列表的post接口
@RequestMapping(value = "/useSpringBoot/postResponseUsers", method = RequestMethod.POST)
@ApiOperation(value = "攜帶cookie信息請求接口,同時返回用戶列表的post接口", httpMethod = "POST")
public User getusers(HttpServletRequest request, @RequestBody User user) {
//上一個接口返回的cookie,本接口去獲取
Cookie[] cookie=request.getCookies();
for(Cookie cookie1 : cookie){
if(cookie1.getName().equals("token") && cookie1.getValue().equals("1234567890") &&
user.getLoginname().equals("qinzhenxia") && user.getPassword().equals("123456")){
//接口的請求參數(shù)是Loginname和Password
//接口的返回參數(shù)是Loginname、Password、Name、Age、Sex
//請求和返回的格式都是json
//給user類中的屬性賦值并返回
users=new User();
users.setLoginname("qinzhenxia");
users.setPassword("123456");
users.setName("sunqihua");
users.setAge(28);
users.setSex("男");
return users;
}
}
return null;
}
}