在開始之前,我們需要去創建一個SpringBoot項目,大家可以去 http://start.spring.io/ 這個網站生成一個項目。 如圖,這邊可以對SpringBoot項目進行詳細設置: 下麵這個web一定要勾選: SpringBoot版本號選擇1.5.10 全部設置好了以後,就點擊這個按鈕 ...
在開始之前,我們需要去創建一個SpringBoot項目,大家可以去 http://start.spring.io/ 這個網站生成一個項目。
![](http://upload-images.jianshu.io/upload_images/1929342-44b6ee3ecdaa5770.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200)
如圖,這邊可以對SpringBoot項目進行詳細設置:
![](http://upload-images.jianshu.io/upload_images/1929342-7b51a6ba4482d6f7.png?imageMogr2/auto-orient/strip|imageView2/2/w/595)
下麵這個web一定要勾選:
![](http://upload-images.jianshu.io/upload_images/1929342-8a62492a518ac264.png?imageMogr2/auto-orient/strip|imageView2/2/w/1137)
![](http://upload-images.jianshu.io/upload_images/1929342-46cc9f25c64cb624.png?imageMogr2/auto-orient/strip|imageView2/2/w/1070)
SpringBoot版本號選擇1.5.10
全部設置好了以後,就點擊這個按鈕:
![](http://upload-images.jianshu.io/upload_images/1929342-30c5947fccd671ad.png?imageMogr2/auto-orient/strip|imageView2/2/w/313)
這邊我就直接下載到桌面了。
![](http://upload-images.jianshu.io/upload_images/1929342-442bb044b8ee5981.png?imageMogr2/auto-orient/strip|imageView2/2/w/565)
![](http://upload-images.jianshu.io/upload_images/1929342-4058a7148cd75b4a.png?imageMogr2/auto-orient/strip|imageView2/2/w/99)
解壓這個壓縮包,我們看下目錄結構:
![](http://upload-images.jianshu.io/upload_images/1929342-4cfe3643ee4d5f9e.png?imageMogr2/auto-orient/strip|imageView2/2/w/782)
可以看到,這是一個maven項目結構,然後,我們要打開eclipse導入這個項目,建議使用eclipse的mars版本,自帶maven插件。
打開eclipse後,導入:
![](http://upload-images.jianshu.io/upload_images/1929342-d5ed545605624d3c.png?imageMogr2/auto-orient/strip|imageView2/2/w/358)
![](http://upload-images.jianshu.io/upload_images/1929342-0f74d0b719003219.png?imageMogr2/auto-orient/strip|imageView2/2/w/597)
![](http://upload-images.jianshu.io/upload_images/1929342-c338d1526498b04f.png?imageMogr2/auto-orient/strip|imageView2/2/w/664)
導入桌面上解壓後的文件夾:
![](http://upload-images.jianshu.io/upload_images/1929342-eec3514d5a950a3f.png?imageMogr2/auto-orient/strip|imageView2/2/w/664)
![](http://upload-images.jianshu.io/upload_images/1929342-9490d3d2c621c84b.png?imageMogr2/auto-orient/strip|imageView2/2/w/596)
第一次導入SpringBoot項目,會自動下載很多jar包,建議替換成阿裡雲的maven倉庫,不然會很慢。
wait... ... ...
建議這個時候你去喝杯咖啡~~ 來一局游戲啥的~~
經過漫長的等待,終於好了!!
![](http://upload-images.jianshu.io/upload_images/1929342-b0cf0ae278e87154.png?imageMogr2/auto-orient/strip|imageView2/2/w/326)
找到這個類:
![](http://upload-images.jianshu.io/upload_images/1929342-176225c69242c6af.png?imageMogr2/auto-orient/strip|imageView2/2/w/448)
這個是SpringBoot項目的啟動類,運行:
![](http://upload-images.jianshu.io/upload_images/1929342-7b4882af916e01fc.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200)
打開瀏覽器,輸入http://localhost:8080/
發現:
![](http://upload-images.jianshu.io/upload_images/1929342-6bb9ce72fb501f68.png?imageMogr2/auto-orient/strip|imageView2/2/w/770)
項目給我們拋了一個404,這個是當然的了,因為我們確實什麼都沒有寫啊。
現在,建一個controller包:
![](http://upload-images.jianshu.io/upload_images/1929342-1a70e6ca46cb8aa1.png?imageMogr2/auto-orient/strip|imageView2/2/w/328)
建一個HelloController類:
![](http://upload-images.jianshu.io/upload_images/1929342-b0c68f3e28e0d003.png?imageMogr2/auto-orient/strip|imageView2/2/w/271)
代碼:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "<font style='font-size:28px;'>Hello Spring Boot</font>";
}
}
其中,RestController是一個複合註解,相當於 @Controller和@ResponseBody的組合,相信有SpringMVC基礎的朋友都能夠明白是什麼意思。
這裡就是返回一個字元串 "Hello Spring Boot" 。
重新啟動 DemoApplication 類,在瀏覽器中輸入:
可以看到效果:
![](http://upload-images.jianshu.io/upload_images/1929342-a48330874b60a3d9.png?imageMogr2/auto-orient/strip|imageView2/2/w/295)
您的支持是我寫作的最大動力:
![](http://upload-images.jianshu.io/upload_images/1929342-86f89e1290a35202.png?imageMogr2/auto-orient/strip|imageView2/2/w/491)
作者:剽悍一小兔
鏈接:https://www.jianshu.com/p/1129fba8b782
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。