1、模拟get请求
2、模拟get请求返回cookie
3、模拟get请求携带cookie信息
4、模拟get请求携带参数
5、模拟get请求携带参数二
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 "这是我的返回结果";
}
}
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请求的返回结果";
}
//模拟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的请求结果";
}
}
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请求的返回结果";
}
//模拟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的请求结果";
}
//模拟get请求携带cookie信息
//HttpServletRequest类是一个存储器,可以把cookie等信息加入到请求中去访问get请求
@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)
public String getwithCookies(HttpServletRequest request){
//从get请求携带的cookie中获取cookie,并赋值给cookies数组
Cookie[] cookies = request.getCookies();
//判断cookies数组是否为空,如果为空返回错误,不为空判断cookies是不是获取到的cookies
if(Objects.isNull(cookies)){
return "必须携带cookie信息";
}
//foreach循环cookies,对象类型是cookie类,利用cookie类的getName和getValue方法判断cookies信息
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
return "这是一个携带cookie信息的get请求";
}
}
return "cookies信息校验失败";
}
}
4、模拟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.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请求的返回结果";
}
//模拟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的请求结果";
}
//模拟get请求携带cookie信息
//HttpServletRequest类是一个存储器,可以把cookie等信息加入到请求中去访问get请求
@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)
public String getwithCookies(HttpServletRequest request){
//从get请求携带的cookie中获取cookie,并赋值给cookies数组
Cookie[] cookies = request.getCookies();
//判断cookies数组是否为空,如果为空返回错误,不为空判断cookies是不是获取到的cookies
if(Objects.isNull(cookies)){
return "必须携带cookie信息";
}
//foreach循环cookies,对象类型是cookie类,利用cookie类的getName和getValue方法判断cookies信息
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
return "这是一个携带cookie信息的get请求";
}
}
return "cookies信息校验失败";
}
//模拟get请求携带参数
@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请求携带参数二,使用@PathVariable注解获取参数值
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请求的返回结果";
}
//模拟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的请求结果";
}
//模拟get请求携带cookie信息
//HttpServletRequest类是一个存储器,可以把cookie等信息加入到请求中去访问get请求
@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)
public String getwithCookies(HttpServletRequest request){
//从get请求携带的cookie中获取cookie,并赋值给cookies数组
Cookie[] cookies = request.getCookies();
//判断cookies数组是否为空,如果为空返回错误,不为空判断cookies是不是获取到的cookies
if(Objects.isNull(cookies)){
return "必须携带cookie信息";
}
//foreach循环cookies,对象类型是cookie类,利用cookie类的getName和getValue方法判断cookies信息
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
return "这是一个携带cookie信息的get请求";
}
}
return "cookies信息校验失败";
}
//模拟get请求携带参数
@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请求携带参数 @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信息
以下代码中getusers方法就是携带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";
}
//创建users对象,getusers接口会用到该对象创建用户登录信息。
User users;
//用postman请求Login接口,会自动存储cookie,再请求getusers接口时会自动把cookie带上,所以请求时不需要再单独处理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")){
//接口的请求参数是Loginname和Password
//接口的返回参数是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;
}
}