二.spring boot第一個web服務
- 2020 年 9 月 9 日
- 筆記
- JAVA, spring boot專題
通過《一.spring boot初始化項目》我們已經會初始化spring boot項目,那本篇文章就說明下初始化項目的具體內容,並編寫第一個Hello頁面。
-
項目結構
-
mvnw、mvnw.cmd:Maven包裝器腳本,實現本機不安裝Maven,也可以構建項目
-
pom.xml文件:相信使用maven的同學們應該都知道這個文件的重要性,主要用於引入依賴。本文用例是web項目,所以初始化項目時,添加的依賴是web和thymeleaf,具體如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd"> <!--POM使用的對象模型版本,強制性的,一般很少改變--> <modelVersion>4.0.0</modelVersion> <!--spring boot項目資訊--> <groupId>com.kinglead</groupId> <artifactId>spring-init-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-init-demo</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <!--打包方式,默認是jar--> <!--spring boot資訊--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <!--spring boot版本號--> <relativePath/> <!-- lookup parent from repository --> </parent> <!--版本號維護--> <properties> <java.version>1.8</java.version> </properties> <!--依賴資訊--> <!--spring-boot-starter-xxx是對某一功能的依賴包的集合 用於解決3大問題:1.簡化依賴配置,讓依賴更易管理 2.針對某一功能統一管理,方便依賴引入 3.解決依賴版本衝突問題 --> <dependencies> <!--web項目依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--thymeleaf依賴包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--test--> <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> <!--插件--> <build> <plugins> <!--spring boot maven插件 作用:1.允許使用maven允許應用 2.將所有依賴打入到jar包中 3.在jar包中生成manifest文件,指明引導類 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
SpringInitDemoApplication:spring boot項目啟動類
package com.kinglead.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //@SpringBootApplication註解代表這是一個spring boot應用 //它是一個組合註解 //@SpringBootConfiguration註解將該類聲明為配置類,相當於@Configuration的特殊形式 //@EnableAutoConfiguration啟動spring boot的自動配置 //@ComponentScan啟動組件掃描:將通過@Component、@Controller、@Service這樣註解的類,註冊為spring應用上下文的組件 @SpringBootApplication public class SpringInitDemoApplication { /** * @param args 命令行參數 */ public static void main(String[] args) { SpringApplication.run(SpringInitDemoApplication.class, args); } }
-
resources:資源文件
static:靜態文件目錄,如image、css
templates:模板文件,如html
application.properties:配置文件(開始是空的)
-
SpringInitDemoApplicationTests:測試類
package com.kinglead.demo; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringInitDemoApplicationTests { @Test void contextLoads() { } }
-
-
編寫控制器(Controller)
package com.kinglead.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; /** * @author kinglead * @date 2020-09-08 下午 15:16 * @describe 歡迎頁控制器 */ //@Controller標記類為控制器,能讓spring自動掃描到它,添加到容器中 //另外@RestController = @Controller + @ResponseBody,@ResponseBody標記返回報文是json格式 //@RequestMapping是請求url路徑映射 @Controller @RequestMapping("/") public class HelloController { //@GetMapping get請求方法路徑映射 @GetMapping("/") public String hello(){ return "hello"; //返回的string值,將會被解析為視圖的邏輯名 } }
-
編寫歡迎頁(Hello.html)
<!DOCTYPE html> <html xmlns="//www.w3.org/1999/xhtml" xmlns:th="//www.thymelead.org"> <head> <meta charset="UTF-8"> <title>Spring Demo</title> </head> <body> <h1>Welcome to Spring</h1> <img th:src="@{/images/1.png}"> </body> </html>
-
啟動應用
如果是使用IDEA開發,直接在啟動類上Run或在工具類點擊Run
下面是啟動日誌
-
測試請求
網頁正常顯示hello歡迎介面。