Updated March 28, 2023
Introduction to Spring Boot Port
Spring boot port is nothing but using a different port for the different applications, spring boot uses the default port as 8080 for all applications, if we want to change the default port there are multiple ways available for the same. Spring boot provides many ways to change the configuration of the application, the most important configuration is to change the default port of tomcat. If suppose we are running an application on the default port we cannot start another application on this port, so we need to run an application using a different port. We can change the port in spring boot in different ways.
What is Spring Boot Port?
As we know that spring boot is providing the default tomcat server to run our boot application. The boot tomcat server is run on the default port as 8080.
The below example shows that by default spring boot will use port no 8080 to run the application.
We can also change the port in the boot for our application. We can run more than one application by using different ports in the boot.
There are two types of ports that we can change by using the boot application as follows:
- Custom port
- Random port
Using the application properties file we need to set the custom port number for the server.port property.
Below example shows that set the custom port for our application in the application.properties file as follows:
Example:
Server.port = 8081
Below example shows set the custom port by using application.yml files as follows:
Example:
server:
port: 8081
To set any random port to the boot tomcat server we are using the random port method to set the port. Using this method we can set any random port to our server.
The below example shows that set the random port for our application in the application.properties file as follows:
Example:
Server.port = 0
Below example shows set the random port by using application.yml files as follows:
Example:
server:
port: 0
In the above example we can see that we are using the server port as 0 means at the time of starting tomcat server it will assign the random port to tomcat server. We can also change the port of the boot tomcat server using the command line, to change the boot tomcat server port by using the command line we need to package our boot application in a jar file and need to launch it from the command-line interface. Using a command-line interface we can configure the port number of the server at the time of project deployment.
Spring Boot Change Port
Boot is providing the default port of the tomcat server. We can change this port by using properties files and interface.
Below are the property files and interfaces which are used to change the default port of the tomcat server in boot.
- By using the application.yaml file
- By using the application.properties file
- By using command line argument
- By using the web server factory customizer (WebServerFactoryCustomizer) interface
- By using EmbeddedServerContainerCustomizer (embedded servlet container customizer) interface
1. By using the application.yaml file.
- We can change the default port of boot tomcat server by using application.yml file.
- The below example shows to change the port using application.yml file.
Code:
server:
port: 8081
Output:
2. By using EmbeddedServletContainerCustomizer interface.
- We can change the default port of boot tomcat server by using EmbeddedServletContainerCustomizer interface.
- The below example shows to change the port using EmbeddedServletContainerCustomizer interface.
Code:
@Component
public class EmbeddedPort implements EmbeddedServletContainerCustomizer
{
public void customize(ConfigurableEmbeddedServletContainer container)
{
container.setPort (8099);
}
}
Output:
3. By using WebServerFactoryCustomizer interface.
- We can change the default port of boot tomcat server by using WebServerFactoryCustomizer interface.
- The below example shows to change the port using WebServerFactoryCustomizer interface.
Code:
@Component
public class WebFactory implements WebServerFactoryCustomizer
{
public void customize(ConfigurableWebServerFactory factory)
{
factory.setPort (9001);
}
}
Output:
Using Application Properties Files
While we are changing the port of the tomcat server in the boot application.properties file is the recommended way. Because it’s very easy as compared to other options.
Below is the step to change the default port of the application in the boot as follows:
1. Create a project template using initializer and give the following name to the project metadata.
- In the below step we are providing project group name as com.example, artifact name as spring-boot-port, project name as spring-boot-port, package as jar file, and selecting java version as 11.
Group – com.example
Artifact name – spring-boot-port
Name – spring-boot-port
Description – Project of spring-boot-port
Package name – com.example.spring-boot-port
Packaging – Jar
Java – 11
Dependencies – spring web.
2. After generating the project extract files and open this project by using tool suite.
3. After opening the project using the tool suite check the project and its files.
4. Change the custom port using the application.properties file.
- In this step, we are changing the port of boot tomcat server from default to 8081.
- We are editing the application.properties file by using the following code.
Code:
server.port=8081
Output:
5. Run the application.
- In the below image, we can see that the port is changed from 8080 to 8081. We have defined the custom port as 8081.
6. Change the random port in the application.properties file.
- In this step, we are changing the port of boot tomcat server from default to random.
- We are editing the application.properties file by using the following code.
Code:
server.port=0
Output:
7. Run the application.
- In the below image, we can see that the port is changed from 8080 to 53375. We have defined a random port so it will assign the 53375 port no to boot tomcat server.
Conclusion
Boot provides many ways to change the configuration of the application, the most important configuration is to change the default port of tomcat. Application.properties file is the recommended way to change the port in boot because it’s very easy compared to other options.
Recommended Articles
This is a guide to Spring Boot Port. Here we discuss the introduction, spring boot change port, and using application properties files. You may also have a look at the following articles to learn more –