1. 创建Spring Boot项目
- 选择创建方式
- 配置
- 现在版本
- 然后Next就行了
2. pom文件
-
父项目配置
<!-- 有一个父项目 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
-
引入必要的maven依赖
<dependencies> <!-- 启动器 说白了, 就是Spring Boot的启动场景 Spring Boot 会将所有的功能场景, 都变成一个个的启动器 spring-boot-starter-web: 自动导入Web环境所有的依赖 spring-boot-starter-test: 自动导入测试环境所有的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- web依赖: tomcat, dispatcherServlet, xml --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring boot的依赖都是以spring-boot-starter开头的 --> <!-- 单元测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
3. 程序代码
-
创建一个控制器(HelloController)
package com.hjf.controlller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Jiang锋时刻 * @create 2020-09-20 20:26 */ @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello world"; } }
-
主程序
package com.hjf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringApplication类的作用: * 1. 推断应用的类型是普通的项目还是Web项目 * 2. 查找并加载所有可用初始化器, 设置到initializers属性中 * 3. 找出所有的应用程序监听器, 设置到listeners属性中 * 4. 推送并设置main方法的定义类, 找到运行的主类 */ @SpringBootApplication public class Demo01Application { public static void main(String[] args) { SpringApplication.run(Demo01Application.class, args); } }
-
启动主程序, 运行结果
4. 说明
- Spring Boot 默认端口号是8080, 但是我的8080端口被占用了的, 所以就改为了8081, 在application.properties文件中设置
- 可以通过创建banner.txt文件, 自定义设置启动的banner图像
启动