springMVC中获取URl参数注解
一.注解
@ PathVariable
@RequestParam
@RequestBody
springMVC中获取参数的注解主要为这三个。
@PathVariable
controller映射的路径上具有一个占位符,@PathVariable可以接收请求路径中占位符的值并将之赋值给所注解的参数。
// url:xxx/test/参数
@GetMapping("/test/{name1}")
public String test1(@PathVariable("name1") String name){
return name;
}
访问:http://127.0.0.1:8080/test/110
110
@RequestParam
每个参数都已Key=Value的形式跟在url的后方,@RequestParam可以获取url后面参数中与自己所定义的参数名的值并注入到所注解的参数中。
// url:xxx?name=值&age=值
@GetMapping("test")
public String test2(@RequestParam("name")String name){
return name;
}
访问:http://127.0.0.1:8080/test?name=tom
tom
@RequestBody
多用于content-type为application/json,@RequestBody会获取请求中所携带的json字符串内容并与注解的bean进行参数对比。
// url:xxx 参数为json字符串 POST
@PostMapping("test")
public Person test3(@RequestBody Person person){
return person ;
}
请先 后发表评论~