SpringBoot學習,建立一個簡單的SpringBoot專案,修改Tomcat預設埠和訪問路徑
建立一個簡單的SpringBoot專案
1. 使用IDEA新建一個Maven專案
2.在pom.xml新增SpringBoot的相關依賴
<?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>com.test</groupId> <artifactId>test-springboot-helloworld</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> </parent> <dependencies> <!-- 不需要寫版本號,版本號依賴父專案(spring-boot-starter-parent)管理 --> <!-- SpringBoot 將所有的功能場景抽取出來,做成一個個starter(啟動器), 只需要在專案中引入這些starter相關場景的所有依賴都會匯入進來,要用什麼功能就匯入什麼啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- SpringBoot打包外掛,可以將程式碼打包成一個可執行的jar包 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.新建Application.java
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication 用來標註一個主程式,說明這是一個Spring Boot應用 */ @SpringBootApplication public class Application { public static void main(String[] args) { // Spring應用啟動 SpringApplication.run(Application.class, args); } }
4.新建HelloController.java
package com.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/hello") @ResponseBody public String hello(){ return "Hello World!"; } }
專案框架:
至此,一個簡單的SpringBoot專案構建完成。
然後啟動Application.java來啟動SpringBoot專案,啟動後報錯,錯誤資訊如下:
Description:
The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.
Action:
Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.
原因是因為8080埠被佔用,Tomcat啟動失敗。這個埠被另一個重要的程序佔著,那就修改Tomcat的預設埠。
修改專案訪問埠
使用properties檔案方式:
在src/main/resources目錄下建立:application.properties,新增如下配置即可修改埠號:
server.port=8088
使用yml檔案方式:
在src/main/resources目錄下建立:application.yml,新增如下配置即可修改埠號:
server: port:8088
修改專案訪問路徑
使用properties檔案方式:
在application.properties,新增如下配置即可修改專案訪問路徑
#這是舊版的設定訪問路徑的方法,現在已經不起作用
#server.context-path=/demo
#這是新版設定訪問路徑的方法
server.servlet.context-path=/demo
使用yml檔案方式:
在application.yml,追加如下配置即可修改專案訪問路徑:
server: port:8088
server.context-path:/demo
使用Properties檔案修改埠和路徑:
#設定訪問埠 server.port=8088 #這是舊版的設定訪問路徑的方法,現在已經不起作用 #server.context-path=/demo #這是新版設定訪問路徑的方法 server.servlet.context-path=/demo
修改埠和路徑之後的結果:
開啟http://localhost:8088/demo/,可以看到預設路徑已經從localhost:8088變為localhost:8088/demo/