Spring MVC 注解方式 参数获取 @RequestBody @PathVariable @RequestParam

1 @RequestBody

使用@RequestBody方式获取参数,必须使用post提交方式。如下代码:
@PostMapping("/url")
public String methodA(@RequestBody TeacherQuery teacherQuery) {
    //TeacherQuery是一个对象
}

2 @PathVariable

使用@PathVariable方式获取参数,使用get post delete等提交方式都可以。但是在url中必须使用占位符,如@RequestMapping("/url/{id}")。代码如下:
@DeleteMapping("/url/{id}")
public String methodB(@PathVariable String id) {
    //id接收了前端的传值
}

3@RequestParam

使用@RequestParam方式获取参数,是将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)。代码如下:
 @RequestMapping("/url")
 public String methodB(@RequestParam(value="name",required=true,defaultValue="hello")String name){
      //name接收了前端的传值
    }

你可能感兴趣的:(Spring,java,spring,spring,boot)