springboot 风格post请求接口
一、接收 Form 表单数据
1.app开发定制公司基本的接收方法
package com.example.demo.controller;import com.example.demo.model.Phone;import com.example.demo.model.User;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;import java.io.IOException;import java.util.List;import java.util.Map;/** * RESTful风格POST请求接口 * */@RestControllerpublic class RestfulPostController { /** * POST app开发定制公司app开发定制公司方式传递过来的参数 * 接收 form-data 格式的 POST 数据: *http://127.0.0.1:8080/postNameAge * */ @PostMapping("/postNameAge") public String hello(@RequestParam("name") String name, @RequestParam("age") Integer age) { return "name:" + name + "ge:" + age; }}
- 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
2,app开发定制公司参数没有传递的情况
/** * POST 方式传递过来的参数 * 如果没有传递参数 Controller 将会报错,这个同样有如下两种解决办法: * 使用 required = false 标注参数是非必须的。 * 使用 defaultValue 给参数指定个默认值。 *http://127.0.0.1:8080/postName * */ @PostMapping("/postName") public String postName(@RequestParam(name = "name", defaultValue = "springboot") String name, @RequestParam(name = "age", required = false) Integer age) { return "name:" + name + "ge:" + age; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3,使用 map 来接收参数
/** * POST 方式传递过来的参数 * 使用 map 来接收参数 *http://127.0.0.1:8080/postMap * */ @PostMapping("/postMap") public String postMap(@RequestParam Map<String,Object> params) { return "name:" + params.get("name") + "ge:" + params.get("age"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4,接收一个数组
/** * POST 方式传递过来的参数 * 接收一个数组 *http://127.0.0.1:8080/postArray * */ @PostMapping("/postArray") public String postArray(@RequestParam("name") String[] names) { String result = ""; for(String name:names){ result += name + "\"; } return result; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
5,使用对象来接收参数
(1.)接收单个对象
package com.example.demo.model;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; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
/** * POST 方式传递过来的参数 * 使用对象来接收参数 *http://127.0.0.1:8080/postUser * */ @PostMapping("/postUser") public String postUser(User user) { return "name:" + user.getName() + "ge:" + user.getAge(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
(2.)接收多个对象
package com.example.demo.model;public class Phone { private String number; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
/** * POST 方式传递过来的参数 * 使用多个对象来接收参数 *http://127.0.0.1:8080/postUserPhone * */ @PostMapping("/postUserPhone") public String postUserPhone(User user, Phone phone) { return "name:" + user.getName() + "ge:" + user.getAge() + "
umber:" + phone.getNumber(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
6,使用对象接收时指定参数前缀
如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递:
我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u.
/** * POST 方式传递过来的参数 * 使用对象接收时指定参数前缀 * 如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递: * 我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u. *http://127.0.0.1:8080/postU * */ @PostMapping("/postU") public String postU(@ModelAttribute("u") User user) { return "name:" + user.getName() + "ge:" + user.getAge(); } @InitBinder("u") private void initBinder(WebDataBinder binder) { binder.setFieldDefaultPrefix("u."); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
二、接收字符串文本数据
(1)如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。
/** * POST 方式传递过来的参数 * 如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。 *http://127.0.0.1:8080/postText * */ @PostMapping("/postText") public String postText(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; }
- 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
三、接收 JSON 数据
1,使用 Map 来接收数据
如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map:
/** * POST 方式传递过来的参数 * 接收 JSON 数据 * 使用 Map 来接收数据 * 如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map: *http://127.0.0.1:8080/postRequestMap * */ @PostMapping("/postRequestMap") public String postRequestMap(@RequestBody Map params) { return "name:" + params.get("name") + " age:" + params.get("age"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2,使用 Bean 对象来接收数据
(1.)如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据直接转换成对象:
package com.example.demo.model;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; }}
- 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
/** * POST 方式传递过来的参数 * 接收 JSON 数据 * 使用 Bean 对象来接收数据 * 如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据直接转换成对象: *http://127.0.0.1:8080/postRequestBean * */ @PostMapping("/postRequestBean") public String postRequestBean(@RequestBody User user){ return user.getName() + " " + user.getAge(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
(2)如果传递的 JOSN 数据是一个数组也是可以的,Controller 如下
/** * POST 方式传递过来的参数 * 接收 JSON 数据 * JOSN 数据是一个数组 *http://127.0.0.1:8080/postListUser * */ @PostMapping("/postListUser") public String hello(@RequestBody List<User> users){ String result = ""; for(User user:users){ result += user.getName() + " " + user.getAge() + "\"; } return result; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14