建立Maven工程
使用idea工具建立一個maven工程,該工程為普通的java工程即可
添加SpringBoot的啟動器
SpringBoot要求,項目要繼承SpringBoot的起步依賴spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
指定字元集以及JDK版本
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
SpringBoot要內建SpringMVC進行Controller的開發,是以項目要導入web的啟動依賴
<dependencies>
<!-- Spring Boot web依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
SpringBoot引導類
要通過SpringBoot提供的引導類起步SpringBoot才可以進行通路
//啟動方式一:啟動SpringBootApplication類中的main方法
//啟動方式二:使用Maven指令spring-boot:run執行即可
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Controller
在引導類Application同級包或者子級包中建立Controller
@RestController
public class HelloController {
// http://localhost:8080/hello
@RequestMapping("/hello")
public Map<String, String> sayHello() {
Map<String, String> map = new HashMap<String, String>();
map.put("hello", "springboot");
return map;
}
}
測試
啟動方式一: 啟動Application類中的main方法
啟動方式二: 使用Maven指令spring-boot:run執行即可
通過日志發現,Tomcat started on port(s): 8080 (http) with context path ''tomcat已經起步,端口監聽8080,web應用的虛拟工程名稱為空
打開浏覽器通路
url
位址為:http://localhost:8080/hello
代碼托管:springboot_helloworld