經過如下範例,能夠快速上手使用SpringBoot框架。先來一個HelloWorld小工程。web
本系列教程均採用 IDEA 做爲開發工具,JDK 爲 1.8spring
本例可以使用 PostMan工具來進行測試。PostMan 官網地址 可進行下載。瀏覽器
<!-- 添加SpringBoot 父工程依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent>
<dependencies> <!-- springboot web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
建立包結構信息 com.test.helloworld.controller(存放控制器代碼) 、 com.test.helloworld.app(存放啓動類代碼)springboot
建立控制器類 HelloWorldController 類,代碼以下:app
@RestController public class HelloWorldController { @RequestMapping("/") public String sayHello() { return "hello world!"; } }
HelloWorldController類經過註解 @ResController 可將此類進行轉變,轉變成Rest服務。類中還有一個sayHello函數,此函數經過註解 @RequestMapping 是一個用來處理請求地址映射的註解。框架
@SpringBootApplication public class Application { public static void main(String[] args) { /** * 啓動服務 */ SpringApplication.run(Application.class, args); } }
啓動 Application 類,成功後 經過瀏覽器訪問 http://localhost:8080/函數
界面會呈現出 hello world ! (表示程序完成)spring-boot