上篇链接:Fegin参数使用总结
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
注:一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam。
注:当同时使用@RequestParam()和@RequestBody时,@RequestParam()指定的参数可以是普通元素、数组、集合、对象等等(即:当,@RequestBody 与@RequestParam()可以同时使用时,原SpringMVC接收参数的机制不变,只不过RequestBody 接收的是请求体里面的数据;
而RequestParam接收的是key-value里面的参数,所以它会被切面进行处理从而可以用普通元素、数组、集合、对象等接收)。
即:如果参数时放在请求体中,传入后台的话,那么后台要用@RequestBody才能接收到;如果不是放在请求体中的话,那么后台接收前台传过来的参数时,要用@RequestParam来接收,或则形参前什么也不写也能接收。
注:如果参数前写了@RequestParam(xxx),那么前端必须有对应的xxx名字才行(不管其是否有值,当然可以通过设置该注解的required属性来调节是否必须传),如果没有xxx名的话,那么请求会出错,报400
注:如果参数前不写@RequestParam(xxx)的话,那么就前端可以有可以没有对应的xxx名字才行,如果有xxx名的话,那么就会自动匹配;没有的话,请求也能正确发送。
如果后端参数是一个对象,且该参数前是以@RequestBody修饰的,那么前端传递json参数时,必须满足以下要求: 后端@RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类(即:@RequestBody后面的类)时,会根据json字符串中的key来匹配对应实体类的属性,如果匹配一致且json中的该key对应的值符合(或可转换为),这一条我会在下面详细分析,其他的都可简单略过,
但是本文末的核心逻辑代码以及几个结论一定要看! 实体类的对应属性的类型要求时,会调用实体类的setter方法将值赋给该属性。 json字符串中,如果value为""的话,后端对应属性如果是String类型的,那么接受到的就是"",如果是后端属性的类型是Integer、Double等类型,那么接收到的就是null。 json字符串中,如果value为null的话,后端对应收到的就是null。
以上借鉴链接:https://www.cnblogs.com/ivan5277/articles/11626198.html
以下为测试代码。
//测试代码 //@PostMapping(value = "/sys/api/test01") //public Result<?> test01(String param01,String param02,String param03,String param04); //测试代码 通过 @PostMapping(value = "/sys/api/test02") public Result<?> test02(@RequestParam("param01")String param01,@RequestParam("param02")String param02,@RequestParam("param03")String param03,@RequestParam("param04")String param04); //测试代码 //@GetMapping(value = "/sys/api/test03") //public Result<?> test03(String param01,String param02,String param03,String param04); //测试代码 通过 @GetMapping(value = "/sys/api/test04") public Result<?> test04(@RequestParam("param01")String param01,@RequestParam("param02")String param02,@RequestParam("param03")String param03,@RequestParam("param04")String param04); //测试代码 //@PostMapping(value = "/sys/api/test05") //public Result<?> test05(List<String> strings01,List<String> strings02); //测试代码 通过 @PostMapping(value = "/sys/api/test06") public Result<?> test06(@RequestParam("strings01")List<String> strings01,@RequestParam("strings02")List<String> strings02); //测试代码 //@GetMapping(value = "/sys/api/test07") //public Result<?> test07(List<String> strings01,List<String> strings02); //测试代码 @GetMapping(value = "/sys/api/test08") public Result<?> test08(@RequestParam("strings01")List<String> strings01,@RequestParam("strings02")List<String> strings02); //测试代码 //@PostMapping(value = "/sys/api/test09") //public Result<?> test09(SysDepartTreeModel sysDepartTreeModel01,SysDepartTreeModel sysDepartTreeModel02,SysDepartTreeModel sysDepartTreeModel03); //测试代码 通过 @PostMapping(value = "/sys/api/test10") public Result<?> test010(@RequestParam("sysDepartTreeModel01")SysDepartTreeModel sysDepartTreeModel01,@RequestParam("sysDepartTreeModel02")SysDepartTreeModel sysDepartTreeModel02); //测试代码SysDepartTreeModel //@PostMapping(value = "/sys/api/test13") //public Result<?> test013(@RequestBody SysDepartTreeModel sysDepartTreeModel01,@RequestBody SysDepartTreeModel sysDepartTreeModel02); //测试代码 //@GetMapping(value = "/sys/api/test11") //public Result<?> test011(SysDepartTreeModel sysDepartTreeModel01,SysDepartTreeModel sysDepartTreeModel02,SysDepartTreeModel sysDepartTreeModel03); //测试代码 通过 @GetMapping(value = "/sys/api/test12") public Result<?> test012(@RequestParam("sysDepartTreeModel01")SysDepartTreeModel sysDepartTreeModel01,@RequestParam("sysDepartTreeModel02")SysDepartTreeModel sysDepartTreeModel02); //测试代码 //@GetMapping(value = "/sys/api/test14") //public Result<?> test014(@RequestBody SysDepartTreeModel sysDepartTreeModel01,@RequestBody SysDepartTreeModel sysDepartTreeModel02);
以下是测试结果,以及报错。
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test01("", "", "", "");
} catch (Exception e) {
e.printStackTrace();
}
try {
Result<?> result = iSysBaseAPI.test02("", "", "", "");
System.out.println("02======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test03("", "", "", "");
} catch (Exception e) {
e.printStackTrace();
}
try {
Result<?> result = iSysBaseAPI.test04("", "", "", "");
System.out.println("04======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test05(strings1, strings2);
} catch (Exception e) {
e.printStackTrace();
}
try {
Result<?> result = iSysBaseAPI.test06(strings1, strings2);
System.out.println("06======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test07(strings1, strings2);
} catch (Exception e) {
e.printStackTrace();
}
try {
Result<?> result = iSysBaseAPI.test08(strings1, strings2);
System.out.println("08======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test09(sysDepartTreeModel1, sysDepartTreeModel2, sysDepartTreeModel3);
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 调用时报错 类型转化异常
* no matching editors or conversion strategy found
**/
//Result<?> result = iSysBaseAPI.test010(sysDepartTreeModel1, sysDepartTreeModel2);
//System.out.println("10======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test013(sysDepartTreeModel1, sysDepartTreeModel2);
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
//iSysBaseAPI.test011(sysDepartTreeModel1, sysDepartTreeModel2, sysDepartTreeModel3);
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 调用时报错 类型转化异常
* no matching editors or conversion strategy found
**/
//Result<?> result = iSysBaseAPI.test012(sysDepartTreeModel1, sysDepartTreeModel2);
//System.out.println("12======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
//iSysBaseAPI.test014(sysDepartTreeModel1, sysDepartTreeModel2);
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 启动过程中报错
* java.lang.IllegalStateException: Method has too many Body parameters:
**/
} catch (Exception e) {
e.printStackTrace();
}
补充,进行改造,测试post和get是否支持RequestBody:
//测试代码
@PostMapping(value = "/sys/api/test13")
public Result<?> test013(@RequestBody SysDepartTreeModel sysDepartTreeModel01);
//测试代码
@GetMapping(value = "/sys/api/test14")
public Result<?> test014(@RequestBody SysDepartTreeModel sysDepartTreeModel01);
结果:
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description: 通过
*
**/
Result<?> result = iSysBaseAPI.test013(sysDepartTreeModel1);
System.out.println("13======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
/**
* @Author: qiaochengqiang
* @Date: 2021/12/17
* @Description:
* 现在改成body
* 不支持POST请求方法,支持以下GET
**/
Result<?> result = iSysBaseAPI.test014(sysDepartTreeModel1);
System.out.println("14======="+result.getMessage());
} catch (Exception e) {
e.printStackTrace();
}