POST
url传参
示例:localhost:8080/post/url?name=zs&age=12
与 GET 请求方式相同,接收方式如下:
HttpServletRequest
@PostMapping("url")
public String testEmpty(HttpServletRequest request, HttpServletResponse res) {
Map<String, String[]> paramMap = request.getParameterMap();
System.out.println(Arrays.toString(paramMap.get("name"))); //[zs]
System.out.println(Arrays.toString(paramMap.get("age"))); //[12]
System.out.println(request.getParameter("name")); //zs
System.out.println(request.getParameter("age")); //12
return "URL 传参";
}
普通参数
@PostMapping("url2")
public String testUrl(String name, int age) {
System.out.println(name); //zs
System.out.println(age); //12
return "URL 传参";
}
可以在参数前面标注 @RequestParam 注解,注解的 value 值对应传参时的命名,默认为变量的字面量值。
@RequestParam("name") String strname
普通表单传参
POST 请求与url传参用法一致
@ResquestBody
参数前加上该注解,可以解析请求体 Body 中 JSON 格式的数据
void method(@RequestBody Map map)
void method(@RequestBody Object object)
- application/json时候可用
- form-data、x-www-form-urlencoded时候不可用
- GET 请求不可用
@RequestParam
(@RequestParam Map map)
- application/json时候,json字符串部分不可用
- url中的?后面添加参数即可用
form-data、x-www-form-urlencoded时候可用,但是要将Headers里的Content-Type删掉
(@RequestParam String waterEleId, @RequestParam String enterpriseName)
application/json时候,json字符串部分不可用
- url中的?后面添加参数即可用
form-data、x-www-form-urlencoded时候可用,且参数可以没有顺序(即前端传过来的参数或者url中的参数顺序不必和后台接口中的参数顺序一致,只要字段名相同就可以),但是要将Headers里的Content-Type删掉
(@RequestParam Object object)
不管application/json、form-data、x-www-form-urlencoded都不可用
- GET 不可用
非@RequestBody、@RequestParam
(Map map)
(Object object)
- application/json时候:json字符串部分不可用
- url中的?后面添加参数不可用。
因为没有指定,它也不知道到底是用json字符串部分还是?后面添加参数部分,所以干脆都不可以用
form-data、x-www-form-urlencoded时都不可用
(HttpServletRequest request)
application/json不可用
- form-data、x-www-form-urlencoded时可用
- url中的?后面添加参数即可用
参考:https://blog.csdn.net/weixin_38004638/article/details/99655322
参数校验
- @Null 用于基本数据类型
- @Empty 用于集合类型
- @Blank 用于字符串类型
SpringBoot - component
MVC 接口传参总结
1. URL 地址传参
请求形式:http://localhost:8080/get/url/10086
请求方法:GET
POST
获取参数:
@RequestMapping(value = "url/{id}", method = {RequestMethod.GET, RequestMethod.POST})
public String testPathParam(@PathVariable("id") String id) {
return "URL 传参 " + id;
}
2. URL 拼接参数
请求形式:http://localhost:8080/common/url?name=zhangsan&age=12
请求方法:GET
POST
1. 从 HTTPRequest 请求体中获取参数
@RequestMapping(value = "url", method = {RequestMethod.GET, RequestMethod.POST})
public String testEmpty(HttpServletRequest request) {
Map<String, String[]> paramMap = request.getParameterMap();
return "URL 传参: name=" + request.getParameter("name") + ", age=" + request.getParameter("age");
}
2. @RequestParam,该方式参数不能为空
@RequestMapping(value = "param", method = {RequestMethod.GET, RequestMethod.POST})
public String testParam(@RequestParam("name") String name, @RequestParam Integer age) {
return "URL 传参: name=" + name + ", age=" + age;
}
3. 直接映射,参数可以为空
@RequestMapping(value = "default", method = {RequestMethod.GET, RequestMethod.POST})
public String testDefault(String name, Integer age) {
return "URL 传参: name=" + name + ", age=" + age;
}
4. Java Bean 接收参数,可以为空
@RequestMapping(value = "bean", method = {RequestMethod.GET, RequestMethod.POST})
public String testBean(NameAndAge nameAndAge) {
return "URL 传参: name=" + nameAndAge.getName() + ", age=" + nameAndAge.getAge();
}
3. Form 表单形式传参
请求形式:http://localhost:8080/common/test
请求方法:GET
POST
对应 Postman:
注意:
form-data:支持文件参数
x-www-form-urlencoded:只支持键值对,不支持 GET 请求方式,只支持 POST
用法基本与 2. URL 拼接参数
一致:
- 从 HTTPRequest 请求体中获取(x-www-form-urlencoded GET请求获取值为null)
- @RequestParam,该方式参数不能为空(x-www-form-urlencoded GET请求 400)
- 直接映射,参数可以为空(x-www-form-urlencoded GET请求获取值为null)
- Java Bean 接收参数,可以为空(x-www-form-urlencoded GET请求获取值为null)
4. Body 形式传参
这里以 application/json 为例
请求形式:http://localhost:8080/post/test
请求方法:POST
接收参数:@RequestBody
@RequestMapping(value = "body/bean", method = {RequestMethod.POST})
public String testBodyBean(@RequestBody NameAndAge nameAndAge) {
return "Body 传参: name=" + nameAndAge.getName() + ", age=" + nameAndAge.getAge();
}