上篇文章介紹了springBoot的各類優勢,嗯,它很容易就能搭建一個web應用,那麼具體怎麼作呢?java
那麼咱們簡單的搭建一個hello 的web應用,這應用很是簡單,寫一個controller,而後讓這controller能在瀏覽器裏面訪問就ok了。web
你可使用eclipse,也可使用idea或其餘任何你習慣使用的開發工具,這裏我使用ideaspring
完成後把裏面的pom文件修改爲apache
<?xml version="1.0" encoding="UTF-8"?>
|
這個配置其實很簡單,固然也能夠解釋一下tomcat
固然,你能夠直接照着這些代碼來完成這簡單的功能,我並不建議你如今就來考慮爲何必定要這麼配置,糾結每個配置的意思,是運行,有個直觀的感覺再說。架構
固然,你也能夠看下這配置具體哪來的,後面有空再來看下爲何這麼配置。mvc
新建一個controller,隨便起個名字都行,這裏就叫TorootController吧,新增一個sayhello的方法,並把相關的springmvc註解給加上app
package cn.toroot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by www.Toroot.cn * java資料學習交流羣:523916260 */ @RestController @RequestMapping("/toroot") public class TorootController { @RequestMapping("/hello") public Object sayHello() { return "hello toroot"; } } |
接下來再增長一個啓動類,類名無所謂,咱們這裏就叫作TorootApp吧,其中具體的代碼以下eclipse
package cn.toroot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by www.Toroot.cn * java資料學習交流羣:523916260 */ @SpringBootApplication public class TorootApp { public static void main(String[] args) { SpringApplication.run(TorootApp.class,args); } } |
嗯,main方法有了,那麼直接點運行試試效果唄,運行main方法後,看到。
嗯,很是好,雖然不知道爲何但這webapp已經運行再tomcat裏面了,經過瀏覽器運行下試試。
http://localhost:8080/toroot/hello
是否是感受很是容易,固然這案例也是經過官網改動而成
具體官網案例以下圖所示
官網:http://projects.spring.io/spring-boot/
好了,至於具體的更高級的應用咱們留下次再分享吧,感興趣的同窗能夠java架構師學習交流羣 862039307