注解 | 作用 |
---|---|
@Controller | 处理http请求 |
@RestController | Spring4之后新加的注解,作用和原来的@ResponseBody配合@Controller相同。用于返回Json |
@PathVariable | 获取url中的参数 |
@RequestParam | 获取请求参数的值 |
@RequestMapping | 配置url映射,需要多个时可以将value写成一个集合 |
@GetMapping | 组合注解,相当于@RequestMapping(method = RequestMethod.GET) |
@PostMapping | 组合注解,相当于@RequestMapping(method = RequestMethod.POST) |
示例:
@PathVariable
获取url中所带的参数,如http://127.0.0.1:8081/girl/hello/say/12
@RequestMapping(value = "/say/{id}" ,method = RequestMethod.GET)
public Integer say(@PathVariable("id") Integer id){
return id;
}
@RequestParam
获取url后所带的请求参数,如http://127.0.0.1:8081/girl/hello/say?id=12
@RequestMapping(value = "/say" ,method = RequestMethod.GET)
public Integer say(@RequestParam(value = "id",required = false,defaultValue ="1") Integer myId){
return myId;
}
注:@RequestParam中‘required ’值表示时候可不传入,‘defaultValue ’值表示当不传入是的默认值,必须为String类型
- @RequestMapping
@RequestMapping(value = "/say" ,method = RequestMethod.GET)
等价于
@GetMapping(value = "/say")