在Handler方法的形参中可以写一些其他类型的值
1、request和response
@RestController
public class OtherController {
@RequestMapping("other")
public RespVo other(HttpServletRequest request, HttpServletResponse response) {
//request和response和之前在servlet中doGet和doPost方法,使用操作一样
//但是不建议直接使用request和response开发
HttpSession session = request.getSession();
session.setAttribute("username","songge");
return RespVo.ok();
}
}
cookie不能够直接作为形参,但是能用request获得
@RequestMapping("cookie")
public BaseRespVo cookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println(name + ":" + value);
}
return BaseRespVo.ok();
}
2、session
@RequestMapping("session2")
public BaseRespVo session2(HttpSession session) {
Object username = session.getAttribute("username");
return BaseRespVo.ok(username);
}