目录
- 一、获取URL中的参数@PathVariable
- 二、上传⽂件@RequestPart
- 三、获取Cookie/Session
- 3.1 HttpServletRequest和 HttpServletResponse
- 3.2 获取Cookie
- 3.2.1 使用HttpServletRequest
- 3.2.2 使用注解@CookieValue
- 3.3 设置session
- 3.4 获取session
- 3.4.1 使用HttpServletRequest
- 3.4.2 直接使用HttpSession
- 3.4.3 使用注解@SessionValue
- 四、获取Header
- 4.1 使用HttpServletRequest
- 4.2 使用注解@RequestHeader
一、获取URL中的参数@PathVariable
path variable: 路径变量。
@PathVariable这个注解可以拿到URL中的参数,使用注意:
- 写在方法的参数名前,每个变量都要写。
- 当URL中的变量与方法中的变量同名时,可以不在@PathVariable写上URL的变量名。
- 当要对拿到的URL变量重命名,要在@PathVariable()括号中写上URL的名字。
- URL中的参数必传。
后端代码:
https://i-blog.csdnimg.cn/direct/8d12329b52314f3fb5c3541a3b7ee7da.png" alt="
" />
Postman传参:
https://i-blog.csdnimg.cn/direct/e50f8660a06c4f27a74f91eac76c2549.png" alt="
" />
二、上传⽂件@RequestPart
- 上传文件使用MuMultipartFile类下的transferTo方法。
- 使用@RequestPart注解可以进行重命名。
- 记得抛异常
后端代码:
@RequestMapping("/r2")
public String getFile(@RequestPart("file11") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
file.transferTo(new File("E:/"+ file.getOriginalFilename()));
return "上传成功"+fileName;
}
Postman传参:
- 在Body里面找form-data传File类型。
https://i-blog.csdnimg.cn/direct/0f925dc9af1047e9a4cc9b7ca6020758.png" alt="
" />
三、获取Cookie/Session
3.1 HttpServletRequest和 HttpServletResponse
- HttpServletRequest这个类可以拿到HTTP请求中的东西。
- HttpServletResponse这个类可以拿到HTTP响应中的东西,还可以进行修改。
https://i-blog.csdnimg.cn/direct/f3346bff63f941578f0d50b6d3d114e2.png" alt="
" />
3.2 获取Cookie
获取Cookie有以下两种方式。
3.2.1 使用HttpServletRequest
只需要调用该类下的getCookies方法即可。
@RequestMapping("r3")
public String r3 (HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
return "获取成功";
}
Postman发请求:
https://i-blog.csdnimg.cn/direct/f7c272637ed04f31a78a0d663f96c00c.png" alt="
" />
3.2.2 使用注解@CookieValue
在注解中写下Cookie中变量的名,然后后面跟着要赋值的变量类型与名字。
这种方法每次只能获得一个Cookie变量。
@RequestMapping("/r4")
public String r4(@CookieValue("name") String name) {
return name;
}
Postman发请求:
https://i-blog.csdnimg.cn/direct/675b8c248f3647368a4d185cf6657119.png" alt="
" />
3.3 设置session
使用HttpServletRequest下的getSession方法,拿到sessionId,拿到session。
HttpSession getSession(boolean create);
- 默认为true,如果没有拿到session,返回一个空HttpSession对象。
- 如果为false,没有拿到session,返回一个null。
代码:
@RequestMapping("/r5")
public String setSession(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("name","lisi");
session.setAttribute("age","88");
return "设置成功";
}
3.4 获取session
获取session有以下三种方式。
3.4.1 使用HttpServletRequest
使用HttpServletRequest类下的getSession方法拿到session,
再通过HttpSession 类下的方法getAttribute获取session中的对象 Object getAttribute(String name);
。
代码:
@RequestMapping("/r6")
public String r5(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (null == session) {
return "用户未登录";
} else {
return "用户名:"+(String) session.getAttribute("name") +
"用户年龄"+(String) session.getAttribute("age");
}
}
Postman传参:
https://i-blog.csdnimg.cn/direct/724c8508dd964d9e8428cbafe67f251e.png" alt="
" />
3.4.2 直接使用HttpSession
相当于HttpSession session = request.getSession();
直接在方法参数完成。
@RequestMapping("/r8")
public String r5(HttpSession session ) {
if (null == session) {
return "用户未登录";
} else {
return "用户名:"+(String) session.getAttribute("name") +
"用户年龄"+(String) session.getAttribute("age");
}
}
3.4.3 使用注解@SessionValue
注解中value代表:session中的对象, required 调配没拿到session时返回什么,true返回空HttpSession对象,false返回null。
代码:
@RequestMapping("r7")
public String r6(@SessionAttribute(value = "name",required = false) String name) {
System.out.println(name);
return name;
}
Postman传参:
https://i-blog.csdnimg.cn/direct/51f3f96ab83246c0a5bdf7b7c8bc61ac.png" alt="
" />
四、获取Header
获取session有以下两种方式。
4.1 使用HttpServletRequest
使用HttpServletRequest类下的getHeader方法,方法中填入想要的header中的参数名称,拿到header下的参数。
代码:
@RequestMapping("/r9")
public String getHeader(HttpServletRequest request) {
String header = request.getHeader("user-agent");
return "获取到 "+header;
}
Postman传参:
https://i-blog.csdnimg.cn/direct/e675a3abddf84a348de26c50f4a52b93.png" alt="
" />
4.2 使用注解@RequestHeader
在注解中写下Header中参数的名,然后后面跟着要赋值给的变量类型与名字。
代码:
@RequestMapping("/r10")
public String getHeader2(@RequestHeader("user-agent") String header) {
return "获取到 "+header;
}
Postman传参:
https://i-blog.csdnimg.cn/direct/64bb96dc2c3a4b74be60e1a1deae02f7.png" alt="
" />