SpringBoot基礎01:創建項目和編寫HTTP接口
創建項目
-
創建新項目
選擇File->New Module->Spring Initializr,配置相關參數
)
通過上面步驟完成基礎項目的創建,會自動生成以下文件。
- 程序的主程序類
- 一個application.properties 配置文件
- 一個測試類
生成的”DemoApplication“(項目類)和“DemoApplicationTests”(測試類)都可以直接運行來啟動當前創建的項目,由於目前該項目為配合任何數據訪問或Web模塊,程序會加載完Spring之后結束運行。
此時可通過網頁:http://localhost:8080/ 來訪問此時所運行的項目,判斷項目是否搭建成功
-
pom.xml分析
打開pom.xml,看看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 https://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.6.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lurenj</groupId> <artifactId>springboot-01-helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-01-helloworld</name> <description>Lurenj first springboot project</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>
如上所示,主要有四個部分:
- 項目元數據信息:創建時候輸入的Project Metadata部分,也就是Maven項目的基本元素,包括:groupId、artifactId、version、name、description等
- parent:繼承spring-boot-starter-parent的依賴管理,控制版本與打包等內容
- dependencies:項目具體依賴,這里包含了spring-boot-starter-web用於實現HTTP接口(該依賴中包含了Spring MVC),官網對它的描述是:使用Spring MVC構建Web(包裹RESTful)應用程序的入門者,使用Tomcat作為默認嵌入式容器。spring-boot-starter-test用於編寫單元測試的依賴包。更多功能模塊的使用我們將在后面逐步展開。
- build:構建配置部分。默認使用了spring-boot-maven-plugin,配合spring-boot-starter-parent就可以把Spring Boot應用打包成JAR來直接運行
編寫HTTP接口
- 在主入口程序同級目錄下創建包體 ,新建一個controller包【一定要在同一級目錄下,否則識別不到】
- 在包中新建一個Controller類
package com.lurenj.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//自動裝配
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
//調用業務,接受前端的參數
return "hello world!";
}
}
- 編寫完畢后,從主程序啟動項目,瀏覽器發起請求,查看頁面返回
- 控制台輸出了SpringBoot的banner
- 控制條輸出來Tomcat訪問的端口號
- 訪問hello請求,字符串成功返回!http://localhost:8080/hello
-
將項目打成jar包
-
打成了jar包后,就可以在任何地方運行了
-
訪問地址更改
可以通過@RequestMapping()和@GetMapping()注釋來更改請求地址
package com.lurenj.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; //自動裝配:http://localhost:8080/user/hello @RestController// 相當於 @Controller+@ResponseBody @RequestMapping("/user")//一層 public class HelloController { @GetMapping("/hello")//二層 public String hello(){ //調用業務,接受前端的參數 return "hello world!"; } }
- @RequestMapping可以指定GET、POST請求方式
- @GetMapping等價於@RequestMapping的GET請求方式
-
更改訪問項目端口號
通過application.properties(核心配置文件)調整server.port變量參數更改訪問端口號
-
更改Spring boot banner
在resources文件夾下新建banner.txt將尋找到的資源粘進txt文件中,banner就自動修改了