鲲鹏社区首页
中文
注册
开发者
我要评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

构建并运行Spring Boot项目

目前有三种方式可以构建并运行Spring Boot项目:

  • 参考Spring Boot 2.7.x版本的官方Getting Started指南来构建Spring Boot Web Service项目。
  • 在Windows系统上利用Spring Initializr快速创建一个Spring Boot Web项目,随后将代码或打包好的可执行JAR文件上传至服务器。
  • 遵循本文以下详细操作步骤快速构建和运行项目。
  1. 创建项目基础目录。

    “/home”目录下创建一个名为tomcat-test-01的项目文件夹,并为文件夹设置必要的子目录结构。

    1
    2
    3
    4
    5
    cd /home
    mkdir -p /home/tomcat-test-01/src/main/java/com/example/tomcattest01
    mkdir -p /home/tomcat-test-01/src/main/java/com/example/tomcattest01/demos/web
    mkdir -p /home/tomcat-test-01/src/main/resources
    mkdir -p /home/tomcat-test-01/src/main/resources/static
    
  2. 编写启动类。

    在com.example.tomcattest01包下创建一个名为TomcatTest01Application的Java类,并编写Spring Boot应用的启动代码。

    1. 创建文件。
      1
      vi /home/tomcat-test-01/src/main/java/com/example/tomcattest01/TomcatTest01Application.java
      
    2. “i”进入编辑模式,在文件中添加以下内容。
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      package com.example.tomcattest01;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      @SpringBootApplication
      public class TomcatTest01Application {
      
          public static void main(String[] args) {
              SpringApplication.run(TomcatTest01Application.class, args);
          }
      
      }
      
    3. “Esc”键,输入:wq!,按“Enter”保存并退出编辑。
  3. 编写controller类。

    在com.example.tomcattest01.demos.web包下创建一个名为BasicController的Java类,用于处理Web请求并返回响应。

    1. 打开文件。
      1
      vi /home/tomcat-test-01/src/main/java/com/example/tomcattest01/demos/web/BasicController.java
      
    2. 按“i”进入编辑模式,在文件中添加以下内容。
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      package com.example.tomcattest01.demos.web;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.ResponseBody;
      
      @Controller
      public class BasicController {
          // http://127.0.0.1:8080/hello?name=Mary
          @RequestMapping("/hello")
          @ResponseBody
          public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
              return "Hello " + name;
          }
      }
      
    3. “Esc”键,输入:wq!,按“Enter”保存并退出编辑。
  4. 编写配置文件。

    “src/main/resources”目录下创建一个application.properties文件,用于配置Spring Boot应用的相关属性,如服务端口等。

    1. 创建文件。
      1
      vi /home/tomcat-test-01/src/main/resources/application.properties
      
    2. 按“i”进入编辑模式,在application.properties文件中添加以下内容。
      1
      server.port=8080
      
    3. 可选:Tomcat默认使用HTTP协议,若需配置HTTPS协议,请在文件中添加相应的SSL证书配置,首先检查原application.properties是否存在server.port配置,如果存在则删除,然后在application.properties文件中继续添加以下内容。
      1
      2
      3
      4
      5
      server.port=8443  # 指定端口为8443
      server.ssl.enabled=true  # 使用HTTPS协议
      server.ssl.key-store=classpath:server.p12 # 配置证书路径
      server.ssl.key-store-type=PKCS12   # 配置证书类型
      server.ssl.key-store-password=123456    # 配置证书密码
      
    4. “Esc”键,输入:wq!,按“Enter”保存并退出编辑。
    5. 可选:在配置HTTPS协议后,需要准备SSL证书文件。以下是创建临时自签名证书的步骤。
      1. 使用openssl命令,创建服务器证书密钥文件server.key
        1
        2
        3
        mkdir /home/cert
        cd /home/cert
        openssl genrsa -des3 -out server.key 1024
        

        输入密码:可随意定义,本文将密码设置成123456,后续4.e.ii中会使用到。

      2. 使用根证书私钥生成自签名的根证书server.crt
        1
        openssl req -x509 -new -key server.key -out server.crt
        

        在此过程中,需要填写一些证书请求信息,包括国家代号(如CN代表中国)、省/州全名、城市/地区全名、组织的英文名、组织单位的英文名(可选)、常用名(可选)和电子邮件地址(可选)。

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        Enter pass phrase for server.key: # 输入上述步骤中的密码,123456
        You are about to be asked to enter information that will be incorporated
        into your certificate request.
        What you are about to enter is what is called a Distinguished Name or a DN.
        There are quite a few fields but you can leave some blank
        For some fields there will be a default value,
        If you enter '.', the field will be left blank.
        -----
        Country Name (2 letter code) [AU]:  # 国家代号(如CN代表中国)
        State or Province Name (full name) [Some-State]: # 省/州全名(如Beijing)
        Locality Name (eg, city) []: # 城市/地区全名(如Beijing)
        Organization Name (eg, company) [Internet Widgits Pty Ltd]: # 组织的英文名(如Huawei)
        Organizational Unit Name (eg, section) []: # 组织单位的英文名(可选,按回车可跳过)
        Common Name (e.g. server FQDN or YOUR name) []: # 常用名(可选,按回车可跳过)
        Email Address []: # 电子邮件地址(可选,按回车可跳过)
        
      3. 生成无密码的私钥private.pem
        1
        openssl genpkey -algorithm RSA -out private.pem
        
      4. 创建证书请求文件server.csr
        1
        openssl req -new -key private.pem -out server.csr
        

        在此过程中,需要填写一些证书请求信息,包括国家代号(如CN代表中国)、省/州全名、城市/地区全名、组织的英文名、组织单位的英文名(可选)、常用名(可选)和电子邮件地址(可选)。

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        You are about to be asked to enter information that will be incorporated
        into your certificate request.
        What you are about to enter is what is called a Distinguished Name or a DN.
        There are quite a few fields but you can leave some blank
        For some fields there will be a default value,
        If you enter '.', the field will be left blank.
        -----
        Country Name (2 letter code) [AU]: # 国家代号(如CN代表中国)
        State or Province Name (full name) [Some-State]: # 省/州全名(如Beijing)
        Locality Name (eg, city) []: # 城市/地区全名(如Beijing)
        Organization Name (eg, company) [Internet Widgits Pty Ltd]:  # 组织的英文名(如Huawei)
        Organizational Unit Name (eg, section) []:  # 组织单位的英文名(可选,按回车可跳过)
        Common Name (e.g. server FQDN or YOUR name) []: # 常用名(可选,按回车可跳过)
        Email Address []:  # 电子邮件地址(可选,按回车可跳过)
        
        Please enter the following 'extra' attributes
        to be sent with your certificate request
        A challenge password []:   # 输入密码:123456
        An optional company name []:   # 公司名(可选,按回车可跳过)
        
      5. 签署证书。

        使用根证书server.crt的私钥和证书请求文件server.csr签署证书,生成最终的证书文件server.crt

        1
        openssl x509 -req -in server.csr -CA server.crt -CAkey server.key -CAcreateserial -out server.crt
        

        在此过程中,需要输入之前设置的密钥密码。

      6. 生成p12文件server.p12,并设置密码。
        1
        openssl pkcs12 -export -in server.crt -inkey private.pem -out server.p12
        

        在此过程中,需要设置密码,例如设置为123456。

      7. 将p12文件移动至Spring Boot配置文件目录。
        1
        mv ./server.p12 /home/tomcat-test-01/src/main/resources
        

        “/home/tomcat-test-01/src/main/resources”为Spring Boot应用的配置文件目录,请根据实际情况填写。

  5. 创建静态页面。
    1. “src/main/resources/static”目录下创建一个名为index.html的静态页面文件。
      1
      vi /home/tomcat-test-01/src/main/resources/static/index.html
      
    2. 按“i”进入编辑模式,在文件中添加以下内容。
      1
      2
      3
      4
      5
      6
      <html>
      <body>
      <h1>hello word!!!</h1>
      <p>this is a html page</p>
      </body>
      </html>
      
    3. “Esc”键,输入:wq!,按“Enter”保存并退出编辑。
  6. 在项目的根目录下创建一个pom.xml文件,用于定义项目的依赖和构建配置。
    1. 创建文件。
      1
      2
      cd /home/tomcat-test-01
      vi pom.xml
      
    2. “i”进入编辑模式,在文件中添加以下内容。
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      <?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>
          <groupId>com.example</groupId>
          <artifactId>tomcat-test-01</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>tomcat-test-01</name>
          <packaging>jar</packaging>
          <description>tomcat-test-01</description>
          <properties>
              <java.version>1.8</java.version>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <spring-boot.version>2.7.6</spring-boot.version>
          </properties>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <version>${spring-boot.version}</version>
                  <scope>test</scope>
              </dependency>
          </dependencies>
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-dependencies</artifactId>
                      <version>${spring-boot.version}</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <configuration>
                          <source>1.8</source>
                          <target>1.8</target>
                          <encoding>UTF-8</encoding>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <version>${spring-boot.version}</version>
                      <configuration>
                          <fork>true</fork>
                          <mainClass>com.example.tomcattest01.TomcatTest01Application</mainClass>
                      </configuration>
                      <executions>
                          <execution>
                              <id>repackage</id>
                              <goals>
                                  <goal>repackage</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      </project>
      
    3. “Esc”键,输入:wq!,按“Enter”保存并退出编辑。
  7. 构建可执行JAR文件。
    1
    mvn clean package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
    

    返回BUILD SUCCESS,说明构建成功。

  8. 运行与验证。
    1. 如果配置了HTTP和HTTPS代理,请先取消HTTP/HTTPS代理配置。
      1
      2
      unset http_proxy
      unset https_proxy
      
    2. 运行JAR文件。
      1
      java -jar target/tomcat-test-01-0.0.1-SNAPSHOT.jar
      

      从下图红框部分可以看出,Spring Boot 2.7.6默认启动了内置的Tomcat 9.0.69。

    3. 验证应用与启动。

      切换至另一个终端窗口,运行curl命令访问应用的默认端口(如http://localhost:8080)。

      1
      curl -v http://localhost:8080
      

      若配置了HTTPS协议,则使用如下命令进行访问。

      1
      curl -k -v https://localhost:8443
      

      若返回如下图所示的预期响应,则说明Tomcat已成功启动,且Spring Boot应用正在正常运行。

    4. 可选:通过Windows访问。
      • 如果个人PC与服务器属同一网段,或个人PC可配置服务器代理,则可通过浏览器访问Tomcat服务器的IP地址和端口。
      • 如果无法访问,需要检查个人PC与服务器网段是否一致,或检查“设置 > 网络和Internet > 代理 > 使用代理服务器”路径下的配置是否则正确。