1.创建一个新项目,记得勾选web选项
2.idea会自动创建一个启动引导类,运行这个项目可以启动Tomcat。
3.书写一个hello类
package com.example.springbootdemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//表示spring某个类的是否可以接收HTTP请求
@RestController
public class helloController {
@RequestMapping("/say")
public String hello(){
return "hello world";
}
}
@RestController继承@Controller@Responsebody,等于在类上同时添加以上两个注解。
@Controller表示这个一个controller类
@Responsebody表示方法的返回值会被放到查询的response中。
需要注意的是:Application启动类的位置:要将Application类放在最外侧,即包含所有子包 ,spring-boot会自动加载启动类所在包下及其子包下的所有组件。
4.之后在浏览器中输入http://localhost:8080/say即可访问到页面。