SpringBoot 中常用注解及各种注解作用 SpringBoot 框架中,注解扮演着非常重要的角色,它们可以帮助开发者快速构建应用程序,提高开发效率。下面将对 SpringBoot 中常用注解进行详细的介绍,并解释每个注解的作用。 @PathVaribale 注解 @PathVaribale 注解用于获取 URL 中的数据,例如,在 URL 中的 `localhost:8080/hello/{id}` 中,`{id}` 就是一个变量,使用 `@PathVaribale` 注解可以获取该变量的值。例如: ```java @RestController public class HelloController { @RequestMapping(value="/hello/{id}", method= RequestMethod.GET) public String sayHello(@PathVariable("id") Integer id){ return "id:" + id; } } ``` 在上面的代码中,`@PathVariable` 注解用于获取 URL 中的 `{id}` 变量的值,并将其传递给 `sayHello` 方法。 @RequestParam 注解 @RequestParam 注解用于获取请求参数的值,例如,在 URL 中的 `localhost:8080/hello?id=1000` 中,`id` 就是一个请求参数,使用 `@RequestParam` 注解可以获取该参数的值。例如: ```java @RestController public class HelloController { @RequestMapping(value="/hello", method= RequestMethod.GET) public String sayHello(@RequestParam("id") Integer id){ return "id:" + id; } } ``` 在上面的代码中,`@RequestParam` 注解用于获取请求参数 `id` 的值,并将其传递给 `sayHello` 方法。 @GetMapping 注解 @GetMapping 注解是一个组合注解,是 `@RequestMapping(method = RequestMethod.GET)` 的缩写。该注解将 HTTP Get 请求映射到特定的处理方法上。例如: ```java @RestController public class HelloController { @GetMapping(value = "/hello") public String sayHello(@RequestParam(value="id", required = false, defaultValue = "1") Integer id){ return "id:" + id; } } ``` 在上面的代码中,`@GetMapping` 注解用于将 HTTP Get 请求映射到 `sayHello` 方法上,并将请求参数 `id` 的值传递给该方法。 @RestController 注解 @RestController 注解是 `@ResponseBody` 和 `@Controller` 的组合注解。该注解用于标识控制器类,并将响应数据直接写回客户端。例如: ```java @RestController public class HelloController { @RequestMapping(value="/hello", method= RequestMethod.GET) public String sayHello(){ return "hello"; } } ``` 在上面的代码中,`@RestController` 注解用于标识 `HelloController` 类,并将响应数据直接写回客户端。 SpringBoot 中常用注解各有其作用,使用合适的注解可以提高开发效率和代码可读性。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rtf](https://img-home.csdnimg.cn/images/20210720083624.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/release/download_crawler_static/12762978/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 3
- 资源: 920
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)