SpringBoot controller获取Post请求的各种方式
0.测试工具postman下载地址:https://www.postman.com/downloads/1.接收 Form 表单数据 form-data1.1 基本的接收方法1.1.1 无注解@PostMapping("/hello")public String hello(String name, Integer age) {String str = "" + name + " : " + ag
0.测试工具postman
下载地址:https://www.postman.com/downloads/
1.接收 Form 表单数据 form-data
1.1 基本的接收方法
1.1.1 无注解
@PostMapping("/hello")
public String hello(String name, Integer age) {
String str = "" + name + " : " + age;
System.out.println(str);
return str;
}
测试1:
测试2:
1.1.2 有注解
@PostMapping("/hello")
public String hello(@RequestParam("id") String name,@RequestParam("age") Integer age) {
String str = "" + name + " : " + age;
System.out.println(str);
return str;
}
测试1:
测试2:报错
同样可以设置默认值和是否为空值
@PostMapping("/hello")
public String hello(@RequestParam(name = "id",required = false) String name,@RequestParam(name = "age",defaultValue = "100") Integer age) {
String str = "" + name + " : " + age;
System.out.println(str);
return str;
}
无参数:
1.2 使用 map 来接收参数
和get请求一样
@PostMapping("/hello")
public String hello(@RequestParam Map<String,Object> params) {
String str = "" + params.get("name") + " : " + params.get("age");
System.out.println(str);
return str;
}
1.3 接收一个数组
@PostMapping("/hello")
public String hello(String[] name) {
String str = "";
for (String s : name){
str = str + s + " ";
}
System.out.println(str);
return str;
}
如果参数为空会报错
1.4 使用对象来接收参数
1.4.1 一个对象
可以直接将多个参数通过 getter、setter 方法注入到对象中去
@PostMapping("/hello")
public String hello(User user) {
String str = "" + user;
System.out.println(str);
return str;
}
User类
public class User {
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public User() {
}
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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
若参数名不匹配
1.4.2 多个对象
@PostMapping("/hello")
public String hello(User user, Phone phone) {
String str = "" + user + " " + phone;
System.out.println(str);
return str;
}
Phone类
public class Phone {
private String number;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Phone() {
}
@Override
public String toString() {
return "Phone{" +
"number='" + number + '\'' +
'}';
}
public Phone(String number) {
this.number = number;
}
}
1.4.3 使用对象接收时指定参数前缀
@PostMapping("/hello")
public String hello(@ModelAttribute("u") User user, Phone phone) {
String str = "" + user + " " + phone;
System.out.println(str);
return str;
}
@InitBinder("u")
private void initBinder(WebDataBinder binder){
binder.setFieldDefaultPrefix("u.");
}
2.接收字符串文本数据 Content-Type: text/plain
2.1 HttpServletRequest
如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。
@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.2 @RequstBody注解
@PostMapping("/hello")
public String hello(@RequestBody String text) {
String str = "" + text;
System.out.println(str);
return str;
}
3.接收 JSON 数据 Content-Type: application/json
必须加 @requestbody
3.1 使用 Map 来接收数据
如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map:
@PostMapping("/hello")
public String hello(@RequestBody Map<String,Object> params) {
String str = "" + params.get("name") + " = " + params.get("age");
System.out.println(str);
return str;
}
3.2 使用 Bean 对象来接收数据
3.2.1 单个对象
User类同上
@PostMapping("/hello")
public String hello(@RequestBody User user) {
String str = "" + user;
System.out.println(str);
return str;
}
3.2.2 多个对象
User类前没有加@RequestBody
@PostMapping("/hello")
public String hello(User user, @RequestBody Phone phone) {
String str = "" + user + " " + phone;
System.out.println(str);
return str;
}
phone类识别 User类未识别
3.2.3 对象数组
@PostMapping("/hello")
public String hello(@RequestBody List<User> user) {
String str = "";
for (int i = 0; i < user.size(); i++) {
str = str + user.get(i) + "\n";
}
System.out.println(str);
return str;
}
4.Content-Type: application/x-www-form-urlencoded类型
基本和 Form 表单数据相同 除4.5不同
4.1 简单使用
4.1.1 无注解
@PostMapping("/hello")
public String hello(String name, Integer age) {
String str = "" + name + " : " + age;
System.out.println(str);
return str;
}
测试1:
测试2:
4.1.2 有注解
@PostMapping("/hello")
public String hello(@RequestParam("id") String name,@RequestParam("age") Integer age) {
String str = "" + name + " = " + age;
System.out.println(str);
return str;
}
报错:
同样可以设置默认值和是否为空值
@PostMapping("/hello")
public String hello(@RequestParam(name = "id",required = false) String name,@RequestParam(name = "age",defaultValue = "100") Integer age) {
String str = "" + name + " : " + age;
System.out.println(str);
return str;
}
4.2 使用 map 来接收参数
@PostMapping("/hello")
public String hello(@RequestParam Map<String,Object> params) {
String str = "" + params.get("name") + " : " + params.get("age");
System.out.println(str);
return str;
}
4.3 接收一个数组
@PostMapping("/hello")
public String hello(String[] name) {
String str = "";
for (String s : name){
str = str + s + " ";
}
System.out.println(str);
return str;
}
4.4 接收对象
4.4.1 一个对象
@PostMapping("/hello")
public String hello(User user) {
String str = "" + user;
System.out.println(str);
return str;
}
4.4.2 多个对象
@PostMapping("/hello")
public String hello(User user, Phone phone) {
String str = "" + user + " " + phone;
System.out.println(str);
return str;
}
4.5 字符串接收
@PostMapping("/hello")
public String hello(@RequestBody String params) {
String str = "" + params;
System.out.println(str);
return str;
}
5.总结
5.1 @RequestParam
主要用于get请求和post请求中form-data、application/x-www-form-urlencoded
@RequestParam有三个配置参数:
- required 表示是否必须,默认为 true,必须。
- defaultValue 可设置请求参数的默认值。
- value 为接收url的参数名(相当于key值)。
5.2 @RequestBody
注解@RequestBody接收的参数是来自requestBody中,即请求体。
一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml、text/plain等类型的数据。
6.参考
https://www.hangge.com/blog/cache/detail_2485.html
https://blog.csdn.net/qq_20957669/article/details/89227840
https://cloud.tencent.com/developer/article/1414464
7.上篇地址
https://blog.csdn.net/jumpe_17/article/details/117125508
更多推荐
所有评论(0)