• SpringBoot - Post请求-接收参数


    一、接收Form表单数据

    1.1、基本的接收方法

    1、下面样例 Controller 接收 form-data 格式的 POST 数据:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestParam;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class HelloController {
    7. @PostMapping("/hello")
    8. public String hello(@RequestParam("name") String name,
    9. @RequestParam("age") Integer age) {
    10. return "name:" + name + "\nage:" + age;
    11. }
    12. }

    2、下面是一个简单的测试样例:

    1.2 参数没有传递的情况

    1、如果没有传递参数 Controller 将会报错,这个同样有如下两种解决办法:

    • 使用 required = false 标注参数是非必须的。
    • 使用 defaultValue 给参数指定个默认值。
    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestParam;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class HelloController {
    7. @PostMapping("/hello")
    8. public String hello(@RequestParam(name = "name", defaultValue = "xxx") String name,
    9. @RequestParam(name = "age", required = false) Integer age) {
    10. return "name:" + name + "\nage:" + age;
    11. }
    12. }

    2、下面是一个简单的测试样例:

    1.3 使用 map 来接收参数

    1、Controller 还可以直接使用 map 来接收所有的请求参数:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestParam;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.Map;
    6. @RestController
    7. public class HelloController {
    8. @PostMapping("/hello")
    9. public String hello(@RequestParam Map params) {
    10. return "name:" + params.get("name") + "\nage:" + params.get("age");
    11. }
    12. }

    2、下面是一个简单的测试样例:

    1.4 接收一个数组

    1、表单中有多个同名参数,Controller 这边可以定义一个数据进行接收:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestParam;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.Map;
    6. @RestController
    7. public class HelloController {
    8. @PostMapping("/hello")
    9. public String hello(@RequestParam("name") String[] names) {
    10. String result = "";
    11. for(String name:names){
    12. result += name + "\n";
    13. }
    14. return result;
    15. }
    16. }

    2、下面是一个简单的测试样例:

    1.5 使用对象来接收参数

    1、如果一个 post 请求的参数太多,我们构造一个对象来简化参数的接收方式:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class HelloController {
    6. @PostMapping("/hello")
    7. public String hello(User user) {
    8. return "name:" + user.getName() + "\nage:" + user.getAge();
    9. }
    10. }

    2、User 类的定义如下,到时可以直接将多个参数通过 gettersetter 方法注入到对象中去:

    1. package com.example.demo;
    2. public class User {
    3. private String name;
    4. private Integer age;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public Integer getAge() {
    12. return age;
    13. }
    14. public void setAge(Integer age) {
    15. this.age = age;
    16. }
    17. }

    3、下面是一个简单的测试样例:

    4、如果传递的参数有前缀,且前缀与接收实体类的名称相同,那么参数也是可以正常传递的:

    5、如果一个 get 请求的参数分属不同的对象,也可以使用多个对象来接收参数:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class HelloController {
    6. @PostMapping("/hello")
    7. public String hello(User user, Phone phone) {
    8. return "name:" + user.getName() + "\nage:" + user.getAge()
    9. + "\nnumber:" + phone.getNumber();
    10. }
    11. }

    1.6、使用对象接收时指定参数前缀

    1、如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递:

    2、我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u.

    1. package com.example.demo;
    2. import org.springframework.web.bind.WebDataBinder;
    3. import org.springframework.web.bind.annotation.*;
    4. @RestController
    5. public class HelloController {
    6. @PostMapping("/hello")
    7. public String hello(@ModelAttribute("u") User user) {
    8. return "name:" + user.getName() + "\nage:" + user.getAge();
    9. }
    10. @InitBinder("u")
    11. private void initBinder(WebDataBinder binder) {
    12. binder.setFieldDefaultPrefix("u.");
    13. }
    14. }

    3、重启程序再次发送请求,可以看到参数已经成功接收了:

    二、 接收字符串文本数据

    1)如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。

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

    (2)下面是一个简单的测试样例:

    三、接收 JSON 数据

    3.1 使用 Map 来接收数据

    1)如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestBody;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.Map;
    6. @RestController
    7. public class HelloController {
    8. @PostMapping("/hello")
    9. public String hello(@RequestBody Map params) {
    10. return "name:" + params.get("name") + "\n age:" + params.get("age");
    11. }
    12. }

    (2)下面是一个简单的测试样例:

    3.2 使用 Bean 对象来接收数据

    1)如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据直接转换成对象:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestBody;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class HelloController {
    7. @PostMapping("/hello")
    8. public String hello(@RequestBody User user){
    9. return user.getName() + " " + user.getAge();
    10. }
    11. }

    (2)User 类定义如下:

    1. package com.example.demo;
    2. public class User {
    3. private String name;
    4. private Integer age;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public Integer getAge() {
    12. return age;
    13. }
    14. public void setAge(Integer age) {
    15. this.age = age;
    16. }
    17. }

    (3)下面是一个简单的测试样例:

    4)如果传递的 JOSN 数据是一个数组也是可以的,Controller 做如下修改:

    1. package com.example.demo;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestBody;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.List;
    6. @RestController
    7. public class HelloController {
    8. @PostMapping("/hello")
    9. public String hello(@RequestBody List users){
    10. String result = "";
    11. for(User user:users){
    12. result += user.getName() + " " + user.getAge() + "\n";
    13. }
    14. return result;
    15. }
    16. }

    3.3  接收list对象

    1.请求方式 post json

    [

        {

        "projectId":"1",

        "projectIds":[2,3]

        },

        {

        "projectId":"2",

        "projectIds":[4,5]

        }

    ]

    2. @RequestBody List req

    1. @RequestMapping("/analysis")
    2. public JsonResult queryList (@RequestBody List req) {
    3. ....
    4. }

    3. 结果

  • 相关阅读:
    一文聊透 Netty 核心引擎 Reactor 的运转架构
    Spring Boot 使用 Mail 实现登录邮箱验证
    java从入门到进阶
    文件改名:一键将文件名称改成跟目录名称一样
    【Springcloud微服务】MybatisPlus下篇
    HDI激光钻孔和常见问题
    webrtc vp8/9视频编解码介绍
    Mock和Spy的区别 打桩的区别
    mysql 登录报错 (using password: NO)
    springboot将list封装成csv文件
  • 原文地址:https://blog.csdn.net/hutuyaoniexi/article/details/127657620