前置知識
1.會利用 maven 構建項目
2.了解 Spring 注解
3.了解 RESTful API 的基本理論
4.SpringBoot 是 SpringMVC 的升級版,但兩者沒有必然的聯系
settings.xml 配置
使用國外的 maven 網站下載jar包會很慢,所以建議使用阿里雲的鏡像網站

正式開始我的第一個 SpringBoot HelloWord!
先建立一個普通的 maven 項目,我的命名是 SpringBoot-Demo
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mars</groupId> <artifactId>SpringBoot-Demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringBoot-Demo</name> <url>http://maven.apache.org</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> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- SpringBoot 父容器,這里引入之后,后面的包都不需要添加 version 配置,SpringBoot會自動選擇最適合的版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <!-- scope 的作用域 1、test范圍指的是測試范圍有效,在編譯和打包時都不會使用這個依賴 2、compile范圍指的是編譯范圍有效,在編譯和打包時都會將依賴存儲進去 3、provided依賴:在編譯和測試的過程有效,最后生成war包時不會加入,諸如:servlet-api,因為servlet-api,tomcat等web服務器已經存在了,如果再打包會沖突 4、runtime在運行的時候依賴,在編譯的時候不依賴 --> <dependencies> <!-- SpringBoot 核心包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>compile</scope> </dependency> <!-- SpringBoot 依賴的測試包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> </project> |
配置好之后發現多了兩個類。然而這並不重要

新建一個 Controller 命名為 HelloController
package com.mars.controller; import java.util.HashMap; import java.util.Map; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 我的第一個 SpringBoot HelloWord! * created by Mars on 2017年10月19日 */ @RestController @RequestMapping("hello") public class HelloController { /** * SpringBoot HelloWord! * created by Mars on 2017年10月19日 */ @RequestMapping(value = "/say", method = RequestMethod.GET) public String say(){ return "SpringBoot HelloWord!"; } /** * 測試 JSON 數據 * created by Mars on 2017年10月19日 */ @RequestMapping(value = "/json", method = RequestMethod.GET) public Object getObj(){ Map<String, Object> maps = new HashMap<String, Object>(); maps.put("id", 1); maps.put("name", "mars"); maps.put("say", "SpringBoot HelloWord!"); maps.put("describe", "雖然多余,但還是想測試中文是否有亂碼"); return maps; } } |
新建一個啟動類命名為:RunApplication
package com.mars.run; import java.util.ArrayList; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.mars.controller.HelloController; /** * SpringBoot 啟動類 * 如果使用 @SpringBootApplication 被啟動的類一定要在本類的子包下 * created by Mars on 2017年10月19日 */ @SpringBootApplication public class RunApplication { public static void main(String[] args) { List<Object> list = new ArrayList<Object>(); list.add(HelloController.class); SpringApplication.run(list.toArray(), args); } } |
最后運行啟動類的 main() 方法結果如下:

