環(huán)境準(zhǔn)備
- Java 8
java -version
#我使用的是openjdk
openjdk version "1.8.0_191-1-redhat"
...
- Maven 3
mvn -v
Apache Maven 3.5.4
...
創(chuàng)建Maven項目
- 執(zhí)行mvn命令:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.sample -DartifactId=spring-boot-demo
-
查看新創(chuàng)建的Maven項目:
- 查看App類尘应;
package com.mycompany.sample;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
- 查看pom.xml森渐;
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sample</groupId>
<artifactId>spring-boot-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
以上是默認(rèn)Maven項目的樣子,test就先不介紹了牧氮。
將Maven項目變成Spring Boot項目
- 添加Spring Boot聲明;
#在pom.xml的根節(jié)點(diǎn)添加以下內(nèi)容:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
- 增加Web支持(為后續(xù)開發(fā)API做準(zhǔn)備);
#在pom.xml的dependencies點(diǎn)下添加以下內(nèi)容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
#此處不用再指定版本鲫趁,默認(rèn)跟spring-boot-starter-parent一致,即2.3.1.RELEASE利虫,Spring Boot相關(guān)的依賴是一整套的挨厚。
#pom.xml整體樣子:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sample</groupId>
<artifactId>spring-boot-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-demo</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<repositories>
<repository>
#此處可以使用國內(nèi)Maven鏡像,如aliyun鏡像糠惫、清華大學(xué)鏡像等
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- 修改App類疫剃;
- 在App類上添加注解@SpringBootApplication;
- 在main方法中添加一行代碼硼讽;
#整體代碼
package com.mycompany.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
到此為止巢价,我們已經(jīng)建立了Spring Boot應(yīng)用了,整個項目是可以運(yùn)行的固阁!
只不過在項目中我們還沒有開發(fā)API壤躲,不能通過瀏覽器訪問。
開發(fā)API
- 創(chuàng)建controller包备燃,包名com.mycompany.sample.controller碉克;
-
創(chuàng)建Controller,如HelloWorldController.java并齐;
(注:controller一般就是指寫API的地方漏麦;controller及其他的包、類一般放在App.java同級或下級目錄下)
- 編寫Controller邏輯况褪;
package com.mycompany.sample.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : dylanz
* @since : 06/29/2020
**/
@RestController
public class HelloWorldController {
@GetMapping("/sayHello")
public String sayHello() {
return "Hello World!";
}
@GetMapping("/sayHello/{name}")
public String sayHelloWithNameInPath(@PathVariable String name) {
return "Hello " + name;
}
@GetMapping("/hello")
public String sayHelloWithNameInRequestParam(@RequestParam("name") String name) {
return "Hello " + name;
}
}
啟動項目撕贞,客戶端請求API
- 啟動Spring Boot應(yīng)用;
-
IDEA中直接啟動App.java窝剖;
(注:我們會看到麻掸,啟動App類時,項目自動啟動了tomcat赐纱,Spring Boot已經(jīng)幫我們集成好tomcat了脊奋,多么美好的一件事情0颈薄!3舷丁)
- 瀏覽器中訪問API讶隐;
- http://127.0.0.1:8080/sayHello
- http://127.0.0.1:8080/sayHello/dylan
-
http://127.0.0.1:8080/hello?name=dylan
碼字不容易,點(diǎn)贊需積極
謝謝>糜帧N籽印!