1. 簡介
在近幾年的開發中,spring boot被各種企業,開發人員廣泛的使用,它的設計目的其實很簡單,就是爲了簡化開發,開啓各種自動裝配,不用你在去關注怎麼配置各種文件,jar版本依賴什麼的,只需引入相關的依賴就能迅速搭建起一個web工程,把重點放在業務的實現上。它採用的是建立生產就緒的應用程序觀點,優先於配置的慣例,慢慢的,你會愛上它,相信我。
2. 創建工程所需配置
jdk 1.8及其以上
maven 3.0+IntelliJ IDEA
3.創建步驟
- 第一步:
- 第二步:
- 第三步:
- 第四步:
- 第五步:
4.目錄結構
創建完項目,目錄結構如下:
- src
-main
-java
-package
-SpringbootApplication --------------- 程序的入口
-resouces ------------------------------------ resouces 資源文件
- statics -------------------------------- 靜態資源
- templates ------------------------------ 模板資源
- application.properties ----------------- 配置文件
-test
- pom ------------------------------------------------ pom文件爲基本的依賴管理文件
3.1.pom.xml的依賴
- 4.1 pom.xml的依賴
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!--版本採用的是最新的 2.2.5.RELEASE -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ltw</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 項目名字 以及項目描述-->
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<!--該依賴包含spring-boot-starter,還自動開啓了web功能 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 4.2 主函數 SpringBootApplication
package com.ltw.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 我的第一個springboot程序
*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 4.3 新建一個 HelloWorldController 測試類
package com.ltw.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@ResponseBody
@RequestMapping("/helloWorld")
public String helloWorld() {
return "Hello World! ";
}
}
- 4.4 啓動項目
- 4.5 運行
當控制檯出現如下界面信息時,說明就啓動成功了。
- 4.6 測試結果
當在瀏覽器的地址欄輸入地址,出現Hello World!,說明項目已經成功了。
5. 修改配置文件
我們可以在配置文件(application.properties)裏修改端口號以及訪問路徑等... 當然了,修改後的訪問路徑就是:
http://localhost:9090/ltw/helloWorld
# 默認端口號是 8080 我們可以把它改成 9090
server.port=9090
# 也可以更改訪問路徑
server.servlet.context-path=/ltw
如下顯示
6.結語
還是要說點什麼,不然總覺得不完美...大概就是這樣的,我就不放代碼了,很簡單,相信看過的都會搭建的... 嗯...還有就是不足之處多多包涵,請多指教...
注:如有需要,可自行轉載,但必須加上原創作者及原文章鏈接...