@RequestMapping
@RequestMapping 是 SpringMVC 中的常用注解,功能是用来注册接口的路由映射。
路由映射:当用户访问一个 URL 的时候,用户的请求对应的程序中的某个类的某个方法。
具体用法如下:
@Controller
@ResponseBody
@RequestMapping("/test")
public class testController2 {
@RequestMapping("/test1")
public String execTest(){
return "TEST FINISHED!";
}
}
启动程序后,访问该URL即可:
这里解释一下代码中的另外两个注解:
@Controller
用于将该对象存储到Bean中。
@ResponseBody
因为 SpringMVC 默认是返回一个页面,告诉程序我返回的不是一个页面。
@RestController其实就是上面两个注解的结合。
@RequestMapping默认使用的方法
@RequestMapping中默认使用的是 GET 方法,可以通过以下两种方式选定使用方法
1.使用 method = Request.Method.*:
@Controller
@ResponseBody
@RequestMapping("/test")
public class testController2 {
@RequestMapping(value = "/test1",method = RequestMethod.POST)
public String execTest(){
return "TEST FINISHED!";
}
}
2.使用 @GetMapping 或者@PostMapping 等
如使用 @GetMapping 就相当于使用的是 GET 方法,其它方法如 PUT,DELETE 都有其特定的注解。
@Controller
@ResponseBody
@RequestMapping("/test")
public class testController2 {
@GetMapping("/test1")
public String execTest(){
return "TEST FINISHED!";
}
}
传递参数过程中的常用注解
@RequestParm
该注解主要用于后端对前端参数的重命名,如当前端参数的名字与后端不相同时,可以使用该注解:
//后端参数重命名
//required如果是true则表示参数必须传,否则报错
@RequestMapping("gettime")
public String getTime(@RequestParam(value = "t",required = false) String time){
return time;
}
其中的 required 为设置参数是否必传。
@RequestBody
用于接收 JSON 对象:
//需要加@RequestBody注解才可以得到JSON对象
@RequestMapping("/login3")
public HashMap<String,Object> getParam6(@RequestBody User user){
HashMap<String,Object> result = new HashMap<>();
result.put("name",user.getName());
result.put("password",user.getPassword());
return result;
}
@PathVariable
用于获取URL中的参数:
//从URL中取参数,
@RequestMapping("login4/{name}/{password}")
public String login(@PathVariable("name") String username,@PathVariable String password){
return "username:" + username + "|" + "password:" + password;
}
如果后端参数名与前端参数名不一致,如上代码所示,要在 @PathVariable 中输入前端参数名。
@RequestPart
该注解用于上传文件:
//获取到文件
@RequestMapping("/file")
public String getParam7(String name, @RequestPart("myfile")MultipartFile file) throws IOException {
//保存文件
file.transferTo(new File("C:\\work\\img.png"));
return "success";
}
其中的 "myfile" 对应传输文件的 KEY 值。
@CookieValue
该注解用于获取Cookie:
//获取到Cookie
@RequestMapping("/cookie")
public String getCookie(@CookieValue("Rhaegal") String Rhaegal){
return "Rhaegal~" + Rhaegal;
}
在浏览器中创建Cookie如下图所示:
1.先启动Spring Boot 程序,打开浏览器,点击F12,选择Application,在Cookies中找到 http://localhost:你自己的端口号。
2.添加 Name 和 Value 后重新访问 URl 即可
@SessionAttribute
该注解用于获取 Session,在获取 Session 之前要先设置 Session:
//设置Session
@RequestMapping("/setsess")
public String setSession(HttpServletRequest request){
//获取HttpSession对象,超速设置为true表示如果没有session对象就创建一个session
HttpSession session = request.getSession(true);
if(session != null){
session.setAttribute("username","auto");
}
return "session storage succeeded";
}
//读取Session
@RequestMapping("/getsess")
public String getSession(@SessionAttribute(value = "username",required = true) String username) {
return "username: " + username;
}
@RequestHeader
该注解用于获取 Header 中的参数:
@RequestMapping("/header")
@ResponseBody
public String header(@RequestHeader("User-Agent") String userAgent) {
return "userAgent:"+userAgent;
}