一、RequestBody注解
通过@requestBody可以将请求体中的JSON字符串绑定到相应的bean上,当然,也可以将其分别绑定到对应的字符串上。
json({
url:"/login",
type:“POST”,
data:’{“userName”:“admin”,“pwd”,“admin123”}’,
content-type:“application/json charset=utf-8”,
success:function(data){
alert("request success ! ");
}
});
@requestMapping("/login")
public void login(@requestBody String userName,@requestBody String pwd){
System.out.println(userName+" :"+pwd);
}
这种情况是将JSON字符串中的两个变量的值分别赋予了两个字符串,需要注意的是,JSON字符串中的key必须对应user中的属性名,否则是请求不过去的。
二、PathVariable注解
1.注解解释:@PathVariable 映射 URL 绑定的占位符
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。
举例
@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String name){
return userService.selectUser(name);
}
需要注意的是,方法名称和需要绑定的url中变量名称相同时,可以简写(@PathVariable String name),不太相同时不可以简写(@PathVariable(“name”) String userName)