1.1引入依赖
<dependencies>
<-- 实现springboot的web依赖:实现对 SpringMVC 的自动化配置。同时该依赖会自动帮我们引入 SpringMVC 等相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.2配置文件
在 Spring Boot 项目中,约定通过 application.yml 配置文件,进行 Spring Boot 自动配置的 Bean 的自定义。
在 resource 目录下,创建 application.yaml 配置文件。内容如下:
server:
port: 8081
1.3Demo测试
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/hello")
public String echo() {
return "hello world";
}
}
1.4 Application启动
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
- 添加 @SpringBootApplication 注解,声明是一个 Spring Boot 应用。通过该注解,可以带来 Spring Boot 自动配置等等功能
启动Application类,访问http://localhost:8081/demo/hello,效果如下: