Port Change
By default Spring boot application runs on Apache Tomcat server port 8080. More than one application can not run on same port and it will require change or update the port.
There are multiple ways we can update the port number in Spring Boot.
- Using application.properties file
- using application.yaml file
- using command line parameter
- using EmbeddedServletContainerCustomizer
application.properties File
This is the most recommended method to change or update the port as we can change the same easily. Below is the change require to update the port number.
server.port=8081
Defining a port value as 0 will pick random port number everytime we start server.
server.port=0
application.yaml file
yaml file is also an nother option to change a port number. Below is the change require to update the port number.
Command Line Parameter
We can also change a port number while starting Spring boot application.
Select project root folder and run application as Run Configuration and select project as highlighted below red in color.
Click on below highlighted tab as Arguments and enter required port number:
using EmbeddedServletContainerCustomizer
Create a class with below content and implement EmbeddedServletContainerCustomizer interface and override customize method to set port number.
Right click and run application as Spring Boot App will show below lines on console.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.