1. 創建一個 springboot 項目
使用 idea 創建的基本步驟:
2. 加入父級,起步依賴
pom.xml文件內容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
父級依賴(繼承 springboot 父級項目的依賴):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
起步依賴(springboot 開發web項目的起步依賴,由於添加了父級依賴,起步依賴的版本號與父級版本號一致):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
springboot 提供的項目編譯打包插件:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
3. 創建 springboot 的入口 main 方法
創建后的目錄結構
- 程序入口Application.java須在包的根目錄下,也就是 com.example 下,不能在 demo 或 web 下
程序入口是固定寫法,啟動此程序,啟動spring容器,啟動內嵌的tomcat:
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
4. 創建 Controller
與 springMVC 類似,添加注解訪問即可(demo文件夾下面)
@RestController public class SimpleController { @RequestMapping("/index") public String simple1() { return "你好"; } }
5. 運行 springboot 的 main 方法
運行 main 方法(啟動標識):
訪問成功:
中間報錯解決
解決IDEA Initialization error 'https://start.spring.io'
https://www.cnblogs.com/mike-mei/p/11382765.html