springboot學習demo

來源:互聯網
上載者:User

標籤:tap   demo   瀏覽器   pen   代碼   group   uil   conf   exce   

1,父項目,在pom中加入

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

2,建立子maven項目,

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
</plugin>
</plugins>
</build>

3,如果報錯 Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) on project springboot-base: An exception occurred while running. null: InvocationTargetException: Connector configured to listen on port 8080 failed to start -> [Help 1]

請在子maven項目的pom裡加上紅色標記的plugin

4,啟動後再瀏覽器輸入http://localhost:8080/

5,使用springboot做單元測試

6,springboot 使用的註解:@SpringBootApplication等價於@[email protected]+其他配置

EnableAutoConfiguration:springboot啟動自動設定處理,ComponentScan掃描子包或其他包,

在springboot中,可以把controller單獨抽離到子包中,springboot自動誰掃描到,並執行.

注:剛開始時測試都沒問題,後台加了幾個測試方法後,頁面就一直報錯找不到路徑了,待研究.找到原因後更新

7,路徑映射:@RestController:該註解做自由映射路徑標籤,可以有不同的風格,可以重寫URL,使用該註解時,可以不使用@ResponseBody

8,程式員都會喜歡的倆個依賴:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

這倆個依賴成對使用,主要是在改動某些代碼時,可以不用再重新手動去重新啟動.

注:只是方法和返回的改動時,springboot自動載入,方法參數和註解改動無效.

9,測試幾個方法都沒問題後,可以打包發布了

發布外掛程式:

 

 打包後可以在cmd中使用Java -jar  xxxx.jar,自動執行,無需啟動容器,訪問URL,頁面顯示成功,Linux下也是一樣,不需要容器,只需要一個jar包.

 

springboot學習demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.