参数注解
- 1.@pathVariable的使用:
- 2.@RequestBody
- 3.@RequestParam的使用
- 4.@RequestPart
1.@pathVariable的使用:
设置动态路由:获取URL中的参数:
首先设置一个MethodController类
package com.example.demo.controller;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/met")
public class MethodController {
@GetMapping("/people/{pid}/animal/{aid}")//设置动态路由
public String method1(@PathVariable String pid,
@PathVariable("aid") String aninmalId){
return "用户ID:"+pid+",动物ID:"+aninmalId;
}
}
在前端直接访问:
优点:
1.url语义明确
2.搜索引擎的权限更高;
2.@RequestBody
限制传递参数;在前端请求中传递参数;当请求的数据类型Content-Type为 application/json 时,需要显示的使用@RequestBody注解。
在MethodController中新定义一个方法
@RequestMapping("/getname")
public String method2(String name){
return "姓名:"+name;
}
首先给其传递参数:
取消参数:
使用注解:
@RequestMapping("/login")
public String method3(@RequestBody String name,String password){
return "姓名:"+name+"密码:"+password;
}
注意使用这个注解,测试时请求应该包含在post的body中;
否则访问就会出现错误;RequestBody会查询body中的变量而不是url
使用postman发送请求:
关于@RequestBody我们可以添加参数,required = true表示此参数必须传递,required = false表示可以不传递
@RequestMapping("/login")
public String method3(@RequestBody(required = true) String name,String password){
return "姓名:"+name+"密码:"+password;
}
验证其作用:
设置为false:
设置为true:
只设置key不设置value:
这表示@RequestBody可以传递空字符串参数;
在默认情况下,required=true;
3.@RequestParam的使用
当请求数据要绑定到某个简单对象时,可以使用@RequestParam。
URL 中的请求数据queryString
请求头,Content-Type为表单默认提交的格式 application/x-www-form-urlencoded ,请求体中的数据
请求头,Content-Type为 multipart/form-data ,请求体中的数据。 form-data 可以提交文本数据,也可以提交二进制文件。
以上简单对象包括:基本数据类型、包装类型、MultipartFile(接收二进制文件)
需要注意@RequestParam注解参数默认为 required=true ,如果不传该参数就会报错,需要指定为:@RequestParam(required = false) 。
首先了解RequestParam的指定必须参数是否和@RequestBody相同:
构建方法:
@RequestMapping("/reg2")
public String reg(@RequestParam String username,
@RequestParam String password,
@RequestParam String img){
return "username:"+username+",password:"+password+",img"+img;
}
在required = true和false的情况下,两者是相同的要求,且在只有key没有value的情况下也能传递参数;
当我们传递一个对象时:
@RequestMapping("/reg3")
public String reg3(User user){
return "username:"+user.getUsername()+
"password:"+user.getPassword()+
"img:"+user.getImg();
}
默认可以不传递参数;当我们要求必须传递参数时使用@RequestParam
不传递参数就会报400的错误;
这里表示:没有传递user参数;添加参数进行传递:
此出依然表示需要接受一个type User的参数;
至此,如何传递一个对象呢,可以通过传递Json字符串的形式传递对象,spring能够将json字符串转化为对象;
@RequestMapping("/reg3")
public String reg3(@RequestBody User user){
return "username:"+user.getUsername()+
"password:"+user.getPassword()+
"img:"+user.getImg();
}
4.@RequestPart
对于请求的数据类型Content-Type为 multipart/form-data 时,也就是进行文件上传;
@RequestMapping("/reg")
public class UserController extends DispatcherServlet {
public Object regin(String username,
@RequestPart MultipartFile file) throws IOException {
//1.动态获取当前项目的路径
String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
path+="/upload/";
log.error("我的路径"+path);
//2.创建一个新的文件名(全局唯一id--uuid+文件的原始类型)
String fileType = file.getOriginalFilename();
fileType = fileType.substring(fileType.lastIndexOf("."));
String fileName = UUID.randomUUID().toString()+fileType;
log.error(path+fileName);
//3.将文件保存到服务器
file.transferTo(new File(path+fileName));
return null;
}
}
利用log.error打印出当前项目的路径:
通过postman提交文件和参数:
在/C:/Users/Administrator/IdeaProjects/demo/target/classes/static/upload/底下找到我们上传的文件证明上传成功;