Building and Running a Spring Boot Project
You can build and run a Spring Boot project in any of the following ways:
- Build a Spring Boot web service project by following the Getting Started guide of Spring Boot 2.7.x.
- Use Spring Initializr to quickly create a Spring Boot web project on Windows, and then upload the code (or executable JAR package) to the server.
- Follow the following steps to quickly build and run a project.
- Create a basic project directory.
Create a project folder named tomcat-test-01 in the /home directory and set necessary subdirectories for the folder.
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
- Write the startup class.
Create a Java class named TomcatTest01Application in the com.example.tomcattest01 package and write the startup code of the Spring Boot application.
- Create a file.
1vi /home/tomcat-test-01/src/main/java/com/example/tomcattest01/TomcatTest01Application.java - Press i to enter the insert mode and add the following content to the file:
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); } }
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Create a file.
- Write the controller class.
Create a Java class named BasicController in the com.example.tomcattest01.demos.web package to process web requests and return responses.
- Open the file.
1vi /home/tomcat-test-01/src/main/java/com/example/tomcattest01/demos/web/BasicController.java - Press i to enter the insert mode and add the following content to the file:
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; } }
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Edit the configuration file.
Create an application.properties file in the src/main/resources directory to configure the Spring Boot application properties, such as the service port.
- Create a file.
1vi /home/tomcat-test-01/src/main/resources/application.properties - Press i to enter the insert mode and add the following content to application.properties:
1server.port=8080
- Optional: Tomcat uses HTTP by default. To configure HTTPS, add the corresponding SSL certificate configuration to the file. Check whether the server.port configuration exists in the original application.properties file. If yes, delete the configuration and add the following content to application.properties:
1 2 3 4 5
server.port=8443 # Specifies the port number to 8443. server.ssl.enabled=true # Enables the HTTPS protocol. server.ssl.key-store=classpath:server.p12 # Specifies the certificate path. server.ssl.key-store-type=PKCS12 # Specifies the certificate type. server.ssl.key-store-password=123456 # Specifies the certificate password.
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Optional: Prepare the SSL certificate file after configuring the HTTPS protocol. To create a temporary self-signed certificate, perform the following steps:
- Run the openssl command to create a server certificate key file server.key.
1 2 3
mkdir /home/cert cd /home/cert openssl genrsa -des3 -out server.key 1024
Enter a custom password which will be used in 4.e.ii. This section uses 123456 as an example.
- Use the root certificate private key to generate a self-signed root certificate server.crt.
1openssl req -x509 -new -key server.key -out server.crt
In this step, you are required to fill in some certificate request information, including the country code (for example, CN for China), full name of your province/state, full name of your city/region, name of your organization, name of your organization unit (optional), common name (optional), and email address (optional).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Enter pass phrase for server.key: # Enter 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]: # Country code (for example, CN for China) State or Province Name (full name) [Some-State]: # Full name of a province/state (for example, Beijing) Locality Name (eg, city) []: # Full name of a city/region (for example, Beijing) Organization Name (eg, company) [Internet Widgits Pty Ltd]: # Organization name (for example, Huawei) Organizational Unit Name (eg, section) []: # Organization unit name (Optional. Press Enter to skip this setting.) Common Name (e.g. server FQDN or YOUR name) []: # Common name (Optional. Press Enter to skip this setting.) Email Address []: # Email address (Optional. Press Enter to skip this setting.)
- Generate a private key private.pem without a password.
1openssl genpkey -algorithm RSA -out private.pem
- Create a certificate request file server.csr.
1openssl req -new -key private.pem -out server.csr
In this step, you are required to fill in some certificate request information, including the country code (for example, CN for China), full name of your province/state, full name of your city/region, name of your organization, name of your organization unit (optional), common name (optional), and email address (optional).
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]: # Country code (for example, CN for China) State or Province Name (full name) [Some-State]: # Full name of a province/state (for example, Beijing) Locality Name (eg, city) []: # Full name of a city/region (for example, Beijing) Organization Name (eg, company) [Internet Widgits Pty Ltd]: # Organization name (for example, Huawei) Organizational Unit Name (eg, section) []: # Organization unit name (Optional. Press Enter to skip this setting.) Common Name (e.g. server FQDN or YOUR name) []: # Common name (Optional. Press Enter to skip this setting.) Email Address []: # Email address (Optional. Press Enter to skip this setting.) Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: # Enter the password 123456. An optional company name []: # Company name (Optional. Press Enter to skip this setting.)
- Sign the certificate.
Use the private key of the root certificate server.crt and the certificate request file server.csr to sign the certificate, and generate the final certificate file server.crt.
1openssl x509 -req -in server.csr -CA server.crt -CAkey server.key -CAcreateserial -out server.crt
In this step, enter the key password that is set in previous steps.
- Generate a server.p12 P12 file and set the password.
1openssl pkcs12 -export -in server.crt -inkey private.pem -out server.p12
In this step, set a password, for example, 123456.
- Move the P12 file to the Spring Boot configuration file directory.
1mv ./server.p12 /home/tomcat-test-01/src/main/resources
/home/tomcat-test-01/src/main/resources indicates the configuration file directory of the Spring Boot application. Set it based on your actual requirements.
- Run the openssl command to create a server certificate key file server.key.
- Create a file.
- Create a static page.
- Create a static page file named index.html in the src/main/resources/static directory.
1vi /home/tomcat-test-01/src/main/resources/static/index.html - Press i to enter the insert mode and add the following content to the file:
1 2 3 4 5 6
<html> <body> <h1>hello word!!!</h1> <p>this is a html page</p> </body> </html>
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Create a static page file named index.html in the src/main/resources/static directory.
- Create a pom.xml file in the root directory of the project to define the project's dependencies and build configurations.
- Create a file.
1 2
cd /home/tomcat-test-01 vi pom.xml
- Press i to enter the insert mode and add the following content to the file:
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>
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Create a file.
- Build an executable JAR file.
1mvn clean package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
If "BUILD SUCCESS" is returned, the build is successful.

- Run and verify Tomcat.
- If the HTTP and HTTPS proxies are configured, cancel the HTTP and HTTPS proxy configurations first.
1 2
unset http_proxy unset https_proxy
- Run the JAR file.
1java -jar target/tomcat-test-01-0.0.1-SNAPSHOT.jar
Spring Boot 2.7.6 has started the built-in Tomcat 9.0.69 by default. See the red box in the following figure.

- Verify the application and Tomcat startup status.
Switch to another terminal window and run the curl command to access the default port (for example, http://localhost:8080) of the application.
1curl -v http://localhost:8080
If the HTTPS protocol is configured, run the following command for access:
1curl -k -v https://localhost:8443
If information similar to the following is displayed, Tomcat has been started and the Spring Boot application is running properly.

- Optional: Access through Windows.
- If your PC and the server are in the same network segment, or if your PC can be configured with a server proxy, you can use a browser to visit the IP address and port of the Tomcat server.
- If the Tomcat server fails to be accessed, check whether the network segment of your PC is the same as that of the server, or choose Settings > Network and Internet > Proxy > Use Proxy Server to check whether the configuration is correct.
- If the HTTP and HTTPS proxies are configured, cancel the HTTP and HTTPS proxy configurations first.