File——New——Project,新建項目。
jdk版本1.8即可,聯網環境下選默認 https://start.spring.io
注意:Artfact值只能爲小寫!!
勾選web按鈕。Spring Boot版本選默認即可。
編輯好項目名之後,點擊finish即可。
到此一個Spring Boot項目就創建完成了。
在BlogApplication.java中,右鍵-Run BlogApplication,控制檯看到如下結果,Tomcat運行起來了,項目也初始化成功。
注:Spring Boot 自帶了一個Tomcat.
在瀏覽器上輸入 http://localhost:8080/hello 或者 http://127.0.0.1:8080/hello 來訪問,結果如下:Whitelabel Error Page,出現白頁,並報404錯誤。(有沒有覺得很奇怪??運行正常沒有報錯,頁面卻訪問不到...)
通過查閱資料得知,程序只加載BlogApplication.java(主類)所在包及其子包下的內容。然後檢查HelloController所在的目錄如下,顯然HelloController不在主類BlogApplication.java文件所在包或是其子包下。因此調整目錄如下圖。
將BlogApplication.java文件調整到com.example包下。
將HelloController.java文件調整到com.example.HelloContorller包下。
解決完上一步的問題之後,網頁上再次訪問結果如下:
得到了我們想要的結果。至此,Idea搭建一個完整的入門Spring Boot項目就完成了。
<?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.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>blog</artifactId> <version>0.0.1-SNAPSHOT</version> <name>blog</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>
最後,大家覺得我寫的不錯的,可以關注下哦^v^