POST

url传参

示例:localhost:8080/post/url?name=zs&age=12
与 GET 请求方式相同,接收方式如下:

  1. HttpServletRequest

    1. @PostMapping("url")
    2. public String testEmpty(HttpServletRequest request, HttpServletResponse res) {
    3. Map<String, String[]> paramMap = request.getParameterMap();
    4. System.out.println(Arrays.toString(paramMap.get("name"))); //[zs]
    5. System.out.println(Arrays.toString(paramMap.get("age"))); //[12]
    6. System.out.println(request.getParameter("name")); //zs
    7. System.out.println(request.getParameter("age")); //12
    8. return "URL 传参";
    9. }
  2. 普通参数

    1. @PostMapping("url2")
    2. public String testUrl(String name, int age) {
    3. System.out.println(name); //zs
    4. System.out.println(age); //12
    5. return "URL 传参";
    6. }

    可以在参数前面标注 @RequestParam 注解,注解的 value 值对应传参时的命名,默认为变量的字面量值。@RequestParam("name") String strname

    普通表单传参

    POST 请求与url传参用法一致

@ResquestBody

参数前加上该注解,可以解析请求体 Body 中 JSON 格式的数据

  1. void method(@RequestBody Map map)
  2. void method(@RequestBody Object object)
  1. application/json时候可用
  2. form-data、x-www-form-urlencoded时候不可用
  3. GET 请求不可用

@RequestParam

  1. (@RequestParam Map map)
  1. application/json时候,json字符串部分不可用
  2. url中的?后面添加参数即可用
  3. form-data、x-www-form-urlencoded时候可用,但是要将Headers里的Content-Type删掉

    1. (@RequestParam String waterEleId, @RequestParam String enterpriseName)
  4. application/json时候,json字符串部分不可用

  5. url中的?后面添加参数即可用
  6. form-data、x-www-form-urlencoded时候可用,且参数可以没有顺序(即前端传过来的参数或者url中的参数顺序不必和后台接口中的参数顺序一致,只要字段名相同就可以),但是要将Headers里的Content-Type删掉

    1. (@RequestParam Object object)
  7. 不管application/json、form-data、x-www-form-urlencoded都不可用

  8. GET 不可用

非@RequestBody、@RequestParam

  1. (Map map)
  2. (Object object)
  1. application/json时候:json字符串部分不可用
  2. url中的?后面添加参数不可用。

因为没有指定,它也不知道到底是用json字符串部分还是?后面添加参数部分,所以干脆都不可以用

  1. form-data、x-www-form-urlencoded时都不可用

    1. (HttpServletRequest request)
  2. application/json不可用

  3. form-data、x-www-form-urlencoded时可用
  4. url中的?后面添加参数即可用



参考:https://blog.csdn.net/weixin_38004638/article/details/99655322

参数校验

  1. @Null 用于基本数据类型
  2. @Empty 用于集合类型
  3. @Blank 用于字符串类型

SpringBoot - component

MVC 接口传参总结

1. URL 地址传参

请求形式:http://localhost:8080/get/url/10086

请求方法:GET POST

获取参数:

  1. @RequestMapping(value = "url/{id}", method = {RequestMethod.GET, RequestMethod.POST})
  2. public String testPathParam(@PathVariable("id") String id) {
  3. return "URL 传参 " + id;
  4. }

2. URL 拼接参数

请求形式:http://localhost:8080/common/url?name=zhangsan&age=12

请求方法:GET POST

对应 Postman:


postman-params[1].png

1. 从 HTTPRequest 请求体中获取参数

  1. @RequestMapping(value = "url", method = {RequestMethod.GET, RequestMethod.POST})
  2. public String testEmpty(HttpServletRequest request) {
  3. Map<String, String[]> paramMap = request.getParameterMap();
  4. return "URL 传参: name=" + request.getParameter("name") + ", age=" + request.getParameter("age");
  5. }

2. @RequestParam,该方式参数不能为空

  1. @RequestMapping(value = "param", method = {RequestMethod.GET, RequestMethod.POST})
  2. public String testParam(@RequestParam("name") String name, @RequestParam Integer age) {
  3. return "URL 传参: name=" + name + ", age=" + age;
  4. }

3. 直接映射,参数可以为空

  1. @RequestMapping(value = "default", method = {RequestMethod.GET, RequestMethod.POST})
  2. public String testDefault(String name, Integer age) {
  3. return "URL 传参: name=" + name + ", age=" + age;
  4. }

4. Java Bean 接收参数,可以为空

  1. @RequestMapping(value = "bean", method = {RequestMethod.GET, RequestMethod.POST})
  2. public String testBean(NameAndAge nameAndAge) {
  3. return "URL 传参: name=" + nameAndAge.getName() + ", age=" + nameAndAge.getAge();
  4. }

3. Form 表单形式传参

请求形式:http://localhost:8080/common/test

请求方法:GET POST

对应 Postman:

postman-form[1].png

注意:

form-data:支持文件参数

x-www-form-urlencoded:只支持键值对,不支持 GET 请求方式,只支持 POST

用法基本与 2. URL 拼接参数 一致:

  1. 从 HTTPRequest 请求体中获取(x-www-form-urlencoded GET请求获取值为null)
  2. @RequestParam,该方式参数不能为空(x-www-form-urlencoded GET请求 400)
  3. 直接映射,参数可以为空(x-www-form-urlencoded GET请求获取值为null)
  4. Java Bean 接收参数,可以为空(x-www-form-urlencoded GET请求获取值为null)

4. Body 形式传参

这里以 application/json 为例

请求形式:http://localhost:8080/post/test

请求方法:POST

接收参数:@RequestBody

  1. @RequestMapping(value = "body/bean", method = {RequestMethod.POST})
  2. public String testBodyBean(@RequestBody NameAndAge nameAndAge) {
  3. return "Body 传参: name=" + nameAndAge.getName() + ", age=" + nameAndAge.getAge();
  4. }