@RequestParam 接收路径的参数,形式为 key = value ;
/**
* 接收普通请求参数
* http://localhost/show?name=hello显示为hello
* http://localhost/show?name 显示为null
* @return
*/
@RequestMapping("show")
public String test1(@RequestParam("name") String name){
return name;
}
@PathVariable 接收Resful风格传递的参数,形式为"http://localhost/show/hi/name"。多个参数传入时,顺序要一 一对应。第一个对应第一个,第二个对应第二个。
/**
* 接收普通请求参数
* http://localhost/show/hi/hello 显示为 id + hello
* @return
*/
@RequestMapping("/show/{name1}/{name2}")
public String show(
@PathVariable String name1
@PathVariable String name2) {
retutn name1 + name2;
}
@RequestBody 主要用来接收前端传递给后端的 json 字符串中的数据(即对象);一般使用请求体传参的是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。
在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
/**
* 接收普通请求参数
* http://localhost/show 显示为loginVO 对象封装的信息
* @return
*/
@PostMapping("/show")
public LoginVO show(
@RequestBody LoginVO loginVO) {
retutn loginVO;
}