SpringBoot Web 相关的注解有哪些
SpringBoot Web相关的注解主要有5个。
1. @RequestMapping
映射 web 请求,可以注解在类和方法上,@GetMapping 和 @PostMapping 是 @RequestMapping 的两种特例,一个处理 get 请求,一个处理 post 请求
2. @RequestParam
获取请求参数,示例如下:
```
@RequestMapping("/path1")
@ResponseBody
public String path1(@RequestParam("name")String name1){
System.out.println(name1);
return name1;
}
```
3. @PathVariable
获取路径参数,示例如下:
```
@RequestMapping(value = "user/{username}")
public String test(@PathVariable(value="username") String username) {
return "user"+username;
}
```
4. @RequestBody
通过 HttpMessageConverter 读取 Request Body 并反序列化为 Object,比如直接以 String 接收前端传过来的 json 数据
5. @ResponseBody
将返回值放在 response 体内,返回的是数据而不是页面,在异步请求返回 json 数据时使用
我的笔记