springboot學(xué)習(xí)之HelloWorld篇(附demo)

最近開始學(xué)習(xí)springboot,然后將在博客上記錄和總結(jié)學(xué)習(xí)的過程。這篇主要是講如何搭建springboot的開發(fā)環(huán)境,并且完成HelloWorld程序,使用的開發(fā)工具是IDEA.
springboot官網(wǎng)上對springboot的介紹如下:

Spring Boot makes it easy to create stand-alone, production-grade Spring 
based Applications that you can "just run". We take an opinionated view 
of the Spring platform and third-party libraries so you can get started 
withminimum fuss. Most Spring Boot applications need very little Spring 
configuration.

主要意思就是說springboot只需要很少的配置便可以創(chuàng)建可“運行”的獨立的、生產(chǎn)級的基于Spring的應(yīng)用程序。
springboot的主要特征為:

  • 創(chuàng)建獨立的Spring應(yīng)用程序
  • 直接嵌入Tomcat,Jetty或Undertow(不需要部署WAR文件)
  • 提供有意思的“啟動”POM來簡化您的Maven配置
  • 盡可能自動配置
  • 提供生產(chǎn)就緒功能,如指標(biāo),運行狀況檢查和外部化配置
  • 絕對沒有代碼生成,也不需要XML配置

下面我們開始寫第一個springboot的HelloWorld程序:
一、新建一個Spring Initializr項目


新建一個Spring Initializr項目

二、填寫Group和Artifact名稱


填寫Group和Artifact名稱

三、在Dependensencise里勾選上Web
在HelloWorld階段只需勾選Web,之后還需增加別的依賴
勾選上Web

然后一直點下一步,會自動生成項目初始的結(jié)構(gòu),如下圖所示:



然后新建一個包,可以命名為web,用來放置控制器,編寫HelloWorldController,代碼如下:
package org.vzard.springboot.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by V-zar on 2017/6/28.
 */

@RestController
public class HelloWorldController {
    @RequestMapping
    public String sayHello(){
        return "HelloWorld";
    }

}

最終結(jié)構(gòu)如圖:


然后點擊運行,等待運行成功,打開瀏覽器輸入http://localhost:8080,如果看到如下圖所示,即表明成功:

注:如果失敗,可以查看代碼及依賴是否有誤,和8080端口是否被占用,具體其他問題查看報錯信息分析。

demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容