說明
本篇針對小白,從0到1搭建一個boot運行環(huán)境
前置條件
本地已經(jīng)安裝好jdk8、maven3.4及以上的版本、IDEA開發(fā)工具
步驟
新建工程
打開IDEA,新建bootStart工程,菜單:File->New->Project

新建boot工程導(dǎo)航截圖.png
其余步驟按照提示操作即可!
添加pom依賴
<?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.5.6</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>bootStart</artifactId>
<version>1.0-SNAPSHOT</version>
<name>bootStart</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
添加啟動類
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
添加controller
package org.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StartController {
@RequestMapping("/hello")
public String hello() {
return "{\"key\":\"tom\",\"value\":\"tom\"}";
}
}
運行項目
執(zhí)行StartApplication的main方法,觀察控制臺有啟動信息,待出現(xiàn)如下信息時,標明啟動成功
......
2021-11-03 21:24:36.497 INFO 66169 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 786 ms
2021-11-03 21:24:36.864 INFO 66169 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-03 21:24:36.879 INFO 66169 --- [ main] org.example.StartApplication : Started StartApplication in 1.643 seconds (JVM running for 2.172)
測試
在瀏覽器中輸入: http://localhost:8080/hello 回車出現(xiàn)
{"key":"tom","value":"tom"}
標明服務(wù)可用
至此一個簡單的服務(wù)啟動成功。