在上一节中我们搭建了一个简单的Spring Boot项目。在这一节中我们来根据项目初步了解Spring Boot中常用的注解。
首先在启动类同级目录下新建controller目录,在controller目录中新建java类:DemoController.java
![d0d1e9ea39e963843f18a8ee79120757.png](https://img-blog.csdnimg.cn/img_convert/d0d1e9ea39e963843f18a8ee79120757.png)
项目结构
在DemoController.java中我们利用注解实现一个简单的接口。
@RestControllerpublic class DemoController { @GetMapping("/test") public String test() { return "test"; }}
可以看到在这个类中使用了两个注解,@RestController和 @GetMapping。
启动项目,在浏览器中输入:http://localhost:8080/test
可以看到浏览器中输出test字符。
接下来分析代码。
查看RestController的源码如下:
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Controller@ResponseBodypublic @interface