項(xiàng)目代碼:https://github.com/daydreamer1988/springboot-helloworld
方法一:使用Spring Initializer

New Project

選擇構(gòu)建類型

選擇需要的依賴

解析下載依賴

初始目錄結(jié)構(gòu)
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.minicup</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-helloworld</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>
// 支持Maven打包
</plugin>
</plugins>
</build>
</project>
Application
@SpringBootApplication
public class SpringbootHelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHelloworldApplication.class, args);
}
}
以上就是所有的配置,運(yùn)行main函數(shù)

image.png
打開瀏覽器http://localhost:8080

image.png
服務(wù)是啟動(dòng)了, 但是還沒有接口,接下來創(chuàng)建HelloController.java
@RestController
public class HelloController {
@GetMapping("/hello/{name}")
public String hollo(@PathVariable String name){
return "Hello "+ name;
}
}
重新運(yùn)行

image.png
除了運(yùn)行main函數(shù)之外, 還可以通過命令行執(zhí)行mvn spring-boot:run來啟動(dòng)

image.png
項(xiàng)目打包部署

image.png

image.png
在命令行中輸入java -jar target/springboot-helloworld-0.0.1-SNAPSHOT.jar

image.png
注意, pom文件中的依賴沒有版本號(hào), 這是因?yàn)閜arent的spring-boot-starter-parent中已經(jīng)定義了
方法二:使用普通Maven創(chuàng)建SpringBoot

image.png

image.png

目錄結(jié)構(gòu)
pom.xml

image.png
添加以下依賴(https://spring.io/guides/gs/rest-service/)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<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>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
添加Application類
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加TestController類
@RestController
public class TestController {
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "Hello " + name;
}
}
運(yùn)行main函數(shù),訪問8080,完成
方法三:官網(wǎng)下載類似腳手架項(xiàng)目,IDEA直接打開即可

image.png