SpringBoot小白教程 (一): 使用Maven新建SpringBoot工程
阿新 • • 發佈:2019-01-05
1.執行環境
作業系統:Ubuntu 17
IDE:Eclipse Oxygen
JDK:1.8
2.建立Maven工程
(1)右鍵New -> Project ,在彈出視窗中選擇Maven -> Maven Project 然後點選Next
(2)如圖進行選擇,點選Next
(3)選擇quickstart 1.1 ,點選Next
(4)Group Id 是包名,Artifact Id 是專案名,Package會自動補全,填寫完成後點選Finish
(5)可以看到Maven專案已經建立完成
(6)調整java配置。在專案上點選右鍵,Build Path -> Configure Build Path,彈出如下介面
(7)雙擊Libraries -> JRE System Library[J2SE-1.5] (具體版本可能因環境不用而有差別)。
選擇JavaSE-1.8(jdk 1.8.0_151),點選Finish,然後Apply and Close
(8)編輯pom.xml。
SpringBoot官網目前使用的是1.5.9 (官網地址可以百度)
將下面的程式碼新增到pom.xml中,儲存
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId >
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
(9)在專案上右鍵Maven -> Update Project ,此時Maven將自動下載剛配置的Springboot包
(10)驗證。在src/main/java下的com.study.springbootstudy包中建立SampleController.java類,將官網給的程式碼直接貼上進去(注意修改包名為com.study.springbootstudy),儲存
package com.study.springbootstudy;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
(11)啟動SpringBoot。右鍵 Run As -> Java Application,出現下圖說明啟動成功
(12)訪問頁面。SpringBoot自帶Tomcat,預設埠8080,直接訪問localhost:8080