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/

 

  打開http://localhost:8088/demo/hello

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM