Spring Boot系列之Hello World

何爲Spring Boot這個我就很少介紹了,你們都很熟了,不熟的也不會繼續看下去,熟悉的估計也不會去重複看,有不少人給我說這個東西沒有SpringMVC好,各類不能本身適應啥的,這個是東西是用來幹私活的,不適合作項目等等....這個只能仁者見仁了。好了,下面開始第一篇:Hello World。

寫這個系列的緣由是想本身從頭至尾的梳理一遍用到的東西,溫故知新。之前個人日記都是寫在雲筆記裏的,後來朋友給我說,搜你網名百度前幾個都是你,多寫寫博客得了....web

#開發環境 博主開發工具用的IDEA,管理工具用的SVN,包管理工具是MVN,JDK用的是8,相關設置已經設置好了,編碼UTF-8spring

#建立工程 用IDEA建立工程有兩種方式,一種是maven(gradle),一種是建立Spring Initializr方式,我採用maven,第二種方式很少介紹,你們使用的時候一看就會。apache

  1. 建立項目,在項目下建立一個module(至關於eclipse中的項目),選擇用maven建立以下圖 輸入圖片說明瀏覽器

  2. 不選擇任何骨架,直接Next,輸入相關內容,繼續next 輸入圖片說明springboot

  3. 輸入module的名字,點擊finash結束建立 輸入圖片說明app

  4. 確認一下project的JDK及其編譯級別,還有剛剛建立的module的編譯級別 輸入圖片說明eclipse

  5. 修改pom.xml文件,我知道不少教程也會推薦下面寫法,包括官網都推薦以下寫法,可是我不推薦,緣由呢,除非你能肯定這個就是一個項目,不會有其餘的項目依賴或者父級項目,若是公司要求統一的依賴版本,那勢必會有parent,這樣子與以下寫法相沖突,若是你不想浪費在由於版本不同而致使錯誤上花時間的話不推薦以下寫法,以下maven

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.4.3.RELEASE</version>
</parent>
<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>
  1. 樓主的寫法,以下。整個pom.xml格式,另外說明一點,springboot,若是大家的舊項目使用的1.4.0版本請升級,由於1.4.0會有bug哦~不兼容Druid
<?xml version="1.0" encoding="UTF-8"?>
<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>springboot-note</groupId>
 <artifactId>springboot-hello</artifactId>
 <version>1.0-SNAPSHOT</version>
 <!-- 公共配置信息 -->
 <properties>
     <springframework.version>4.3.3.RELEASE</springframework.version>
     <spring.boot.version>1.4.1.RELEASE</spring.boot.version>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 </properties>
 <!-- 依賴包管理 -->
 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-dependencies</artifactId>
             <version>${spring.boot.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>
 <dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
 </dependencies>
</project>
  1. 新建一個啓動類,以下:說明一下正常來講這個啓動類不會設置爲@RestController的這個地方只是爲了說明一下Hello,同理下面的方法@RequestMapping也不須要寫在這個位置
@RestController
@SpringBootApplication
public class FwApplication {

   public static void main(String[] args) {
       SpringApplication.run(FwApplication.class);
   }
   @RequestMapping(value = "/hello",method = RequestMethod.GET)
   public String sayHello(){
       return "Hello";
   }
}
  1. 啓動main函數,以下圖,啓動成功。 輸入圖片說明函數

  2. 打開瀏覽器輸入http://localhost:8080/hello 返回「Hello」字符串。spring-boot

小彩蛋

  1. 最後給來一個小彩蛋~ 啓動的時候會 有個圖案,好比上面的那個,反正也看不出啥,因此複製下面的字符,在resource目錄下建一個banner.txt的文件,裏面內容以下:貌似沒對齊,不要緊大家本身對齊吧,啓動效果就是這樣子...
┏┓ ┏┓
┏┛┻━━━┛┻┓
┃         ┃
┃     ━    ┃
┃  ┳┛   ┗┳ ┃
┃          ┃
┃     ┻    ┃
┃          ┃
┗━┓      ┏━┛
 ┃     ┃ 神獸保佑
 ┃     ┃ 永不宕機!
 ┃     ┗━━━┓
 ┃          ┣┓
 ┃        ┏┛
 ┗┓┓┏━┳┓┏┛
  ┃┫┫ ┃┫┫
  ┗┻┛ ┗┻┛

輸入圖片說明

相關文章
相關標籤/搜索