As we know that default port for the spring boot application is 8080. But when we want to run more than 1 spring boot application together, we have to change ports because only one service can run on a particular port & we face error that ‘Web server failed to start Port 8080 was already in use’. let’s see how we can change default port of a Spring Boot Application
Changing Server Port in Spring Boot
We can change port number of spring boot server by specifying port no is spring boot properties. As we all know that for changing properties in spring boot we can either user ‘application.yml’ file or ‘application.properties’ file.
you can specify port number in spring boot in the following way by using ‘application.properties’ or ‘application.yml’
application.properties
server.port = 7000
application.yml
server:
port: 7000
after specifying the server port in ‘application.properties’ or ‘application.yml’ the default spring boot port i.e., 8080 is changed. Now Spring Boot application will start on the port no given
Random Sever Port Number in Spring Boot
if you set server port to 0 i.e., server.port =0, then the port number is automatically decided by spring boot, and the application starts on that random port number. Every time application starts, the port number will be a random number
server.port = 0
For live demo of changing spring boot port number you can check this video