In Spring Boot projects, the port of the Tomcat server is usually through a configuration file (or
) to set it. Here are some common configuration methods:
use
existsrc/main/resources/
In the file, you can set the port of Tomcat through the following properties:
=8080
use
If you prefer to use YAML configuration files, you cansrc/main/resources/
Set this in the file:
server: port: 8080
Setting through environment variables
You can also change the port by setting environment variables, which is very useful in containerized applications such as Docker. For example, on Linux or Mac, you can set environment variables before starting the Spring Boot app:
export SERVER_PORT=8080
On Windows, you can use:
set SERVER_PORT=8080
Then launch your Spring Boot app.
Setting through command line parameters
You can also set the port through command line parameters when starting the Spring Boot application:
java -jar --=8080
Or if you use Maven or Gradle plugin to run the application, you can set it up like this
./mvnw spring-boot:run -=--=8080
Or for Gradle:
./gradlew bootRun --args='--=8080'
Set it programmatically (not recommended)
While this is not recommended, as configuration files or environment variables provide better flexibility and maintainability, if you do need to set the port in your code, you can do it by implementing itWebServerFactoryCustomizer
Interface to implement:
import ; import ; import ; @Component public class TomcatPortCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> { @Override public void customize(TomcatServletWebServerFactory factory) { (8080); } }
Choose the best method for your needs to set up the port of Tomcat. Generally, using configuration files or environment variables is the easiest and most straightforward way.
This is the end of this article about springboot configuring tomcat port. For more related springboot tomcat port content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!