目录
1. 注解 @PathVariable
1.1. 方式一
不推荐使用,仅在单个参数的时候选择使用
@GetMapping("/getId/{id}")
@ResponseBody
public String pathVariableTest(@PathVariable Integer id) {
return "id: " + id;
}
1.2. 方式二
推荐使用,适用于单个、多个参数的情况
1.2.1. 单个参数
@GetMapping("/getId/{id}")
@ResponseBody
public String pathVariableTest(@PathVariable("id") Integer id) {
return "id: " + id;
}
1.2.2. 多个参数
@GetMapping("/getIdAny/{id}/{name}")
@ResponseBody
public String pathVariableTestAny(@PathVariable("id") Integer id, @PathVariable("name") String name) {
return "id:" + id + "name:" + name;
}
1.3. 方式三
@GetMapping("/getId/{idValue}")
@ResponseBody
public String pathVariableTest(@PathVariable("idValue") Integer id) {
return "id: " + id;
}
以上三种方式的调用结果都是成功的
2. 注解 @RequestParam
@RequestParam
:将请求参数绑定到你控制器的方法参数上,是 SpringMVC
中接收普通参数的注解
@RequestParam(value = "参数名", required = "true", defaultValue= " ")
value
或name
:参数名required
:是否包含该参数,默认为true
,表示该请求路径中必须包含该参数,如果不包含就报错defaultValue
:默认参数值,如果设置了该值,required = true
将失效,自动为false
,如果没有传该参数,就使用默认值
2.1. 方式一
不推荐使用,仅在单个参数的时候选择使用
@GetMapping("/getId")
@ResponseBody
public