zoukankan      html  css  js  c++  java
  • SpringBoot

        在前文中我介绍了 Controller 如何接收通过 GET 方式传递过来的参数(点击查看),下面接着演示如何接收通过 POST 方式传递过来的参数。

    一、接收 Form 表单数据

    1,基本的接收方法

    (1)下面样例 Controller 接收 form-data 格式的 POST 数据:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestParam("name") String name,
                            @RequestParam("age") Integer age) {
            return "name:" + name + " age:" + age;
        }
    }
     
    (2)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
     

    2,参数没有传递的情况

    (1)如果没有传递参数 Controller 将会报错,这个同样有如下两种解决办法:
    • 使用 required = false 标注参数是非必须的。
    • 使用 defaultValue 给参数指定个默认值。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestParam(name = "name", defaultValue = "xxx") String name,
                            @RequestParam(name = "age", required = false) Integer age) {
            return "name:" + name + " age:" + age;
        }
    }
     
    (2)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
     

    3,使用 map 来接收参数

    (1)Controller 还可以直接使用 map 来接收所有的请求参数:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.util.Map;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestParam Map<String,Object> params) {
            return "name:" + params.get("name") + " age:" + params.get("age");
        }
    }

    (2)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    4,接收一个数组

    (1)表单中有多个同名参数,Controller 这边可以定义一个数据进行接收:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.util.Map;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestParam("name") String[] names) {
            String result = "";
            for(String name:names){
                result += name + " ";
            }
            return result;
        }
    }

    (2)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    5,使用对象来接收参数

    (1)如果一个 post 请求的参数太多,我们构造一个对象来简化参数的接收方式:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(User user) {
            return "name:" + user.getName() + " age:" + user.getAge();
        }
    }

    (2)User 类的定义如下,到时可以直接将多个参数通过 getter、setter 方法注入到对象中去:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.example.demo;
     
    public class User {
        private String name;
        private Integer age;
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public Integer getAge() {
            return age;
        }
     
        public void setAge(Integer age) {
            this.age = age;
        }
    }
     
    (3)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
     
    (4)如果传递的参数有前缀,且前缀与接收实体类的名称相同,那么参数也是可以正常传递的:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    (5)如果一个 get 请求的参数分属不同的对象,也可以使用多个对象来接收参数:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(User user, Phone phone) {
            return "name:" + user.getName() + " age:" + user.getAge()
                    " number:" + phone.getNumber();
        }
    }
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
     

    6,使用对象接收时指定参数前缀

    (1)如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    (2)我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u.
        除了在 Controller 里单独定义预处理方法外,我们还可以通过 @ControllerAdvice 结合 @InitBinder 来定义全局的参数预处理方法,方便各个 Controller 使用。具体做法参考我之前的文章:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package com.example.demo;
     
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.*;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@ModelAttribute("u") User user) {
            return "name:" + user.getName() + " age:" + user.getAge();
        }
     
        @InitBinder("u")
        private void initBinder(WebDataBinder binder) {
            binder.setFieldDefaultPrefix("u.");
        }
    }

    (3)重启程序再次发送请求,可以看到参数已经成功接收了:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    二、接收字符串文本数据

    (1)如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    import javax.servlet.ServletInputStream;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(HttpServletRequest request) {
            ServletInputStream is = null;
            try {
                is = request.getInputStream();
                StringBuilder sb = new StringBuilder();
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = is.read(buf)) != -1) {
                    sb.append(new String(buf, 0, len));
                }
                System.out.println(sb.toString());
                return "获取到的文本内容为:" + sb.toString();
            catch (IOException e) {
                e.printStackTrace();
            finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

    (2)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    三、接收 JSON 数据

    1,使用 Map 来接收数据

    (1)如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.util.Map;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestBody Map params) {
            return "name:" + params.get("name") + " age:" + params.get("age");
        }
    }

    (2)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)

    2,使用 Bean 对象来接收数据

    (1)如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据直接转换成对象:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestBody User user){
            return user.getName() + " " + user.getAge();
        }
    }

    (2)User 类定义如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.example.demo;
     
    public class User {
        private String name;
        private Integer age;
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public Integer getAge() {
            return age;
        }
     
        public void setAge(Integer age) {
            this.age = age;
        }
    }

    (3)下面是一个简单的测试样例:
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
    (4)如果传递的 JOSN 数据是一个数组也是可以的,Controller 做如下修改:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.example.demo;
     
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.util.List;
     
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        public String hello(@RequestBody List<User> users){
            String result = "";
            for(User user:users){
                result += user.getName() + " " + user.getAge() + " ";
            }
            return result;
        }
    }
    原文:SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
     
     
     

    POSTMAN报错信息:

    {
        "msg": "Content type 'text/plain;charset=UTF-8' not supported",
        "code": 500
    }

    解决办法:

    疏忽大意了,需要在postman中切换下数据类型。

     
     
  • 相关阅读:
    mysql安装遇到的坑
    git pull 、git fetch、 git clone
    MD markdown入门
    Libevent:8Bufferevents高级主题
    Libevent:6辅助函数以及类型
    Libevent:5events相关
    Libevent:4event loop
    Libevent:3创建event_base
    Libevent:2设置
    Libevent:1前言
  • 原文地址:https://www.cnblogs.com/netcorner/p/13544443.html
Copyright © 2011-2022 走看看