Updated April 7, 2023
Definition of Spring Boot Change Port
In spring boot project we have the provision to set the port for our application, the default port for spring boot application is 8080, but we can change it to any number we want by making a few configurations into the application file we have. It may happen that we already have some service which is running on port 8080, so in order to avoid the error we have to change the port of the application, it is very useful. Also, we do not need dependency or very heavy configuration to change the port of our application, sprig boot framework made it very easy for us to change it using the yml or property file we have in class path. We can also have a different port for the different environment this also can be changed and done via the application file we have. So whatever the port is written in the application file it will run the application on that specified port only. In the coming section of the tutorial, we will see how we can change the port by using property and yml file in spring boot project for better understanding and usage.
Syntax:
As we already know that we have to make some configuration or need to set some property in order to change the port number in the spring boot framework, let’s take an closer look at the syntax for this see below;
1) for .properties file:
e.g.:
server.port= your_port
2) yml file:
e.g. :
server:
port : your_port
As you can see in the above syntax we are trying to use both properties file and yml file, the difference in the syntax for both. Let’s take a closer look at the practice syntax using properties file in spring boot framework for beginners to get a better understanding see below;
e.g.:
server.port= 8082
How to change port in Spring boot?
As we already know that to we can have any number as the port for spring boot application. This is useful in the scenario where we have so many different services running, so in this case, we cannot run each and every service on the same port because a single port can run single application. Let’s take a scenario to understand the problem in a better way, let’s get started
1) Suppose you have three different spring boot applications running on your local machine, but in order to access them, we required the port, to access the URL and get the data.
2) We have created three different projects and the default port for all those 3 will be 8080, once we have completed the coding part now we would require to test the application.
3) Now in my case, my service 1 interact with the service 2, and request for some data. So in order to access the data, we may need the complete URL of the application which contains the port number as well.
4) Now we have service 1 running on port 8080, now in this case we have to change the port number for both my service that is service 2, and service 3.
5) So spring provides us this provision to be implemented using the property file or yml file in the spring boot framework.
6) below see the flow chart to understand the scenario in a better way.
Flow chart:
Now we will see how we can change the port by using the property file or yml file in spring boot let’s get started;
1) property file: This is the property file that gets created when we create the spring boot project. Also, this file contains all the configuration which are needed, or the user wants to modify the default once. Spring boot provides us so many properties separated by the ‘.’ dot in a property file, after that, we can provide own value we want. Similarly, we have provided change the port of the application, let get started with the code part;
e.g:
server.port=8082
As you can see in the above code written in the property file, we are changing the port for the application from 8080 to 8082, so when we first open the application property file it is blank and no confit will be there initially, we have to make the changes regarding all the configuration we want for our application. So once we started my server the port will not 8082, and we have to access my application with the new port, suppose if we try to make use of the default port after the application start then it will say cannot be reachable.
2) yml file: This is the same file as the application property file with a different extension. Here we specify the value in the format of key-value pair, this gives more understanding of the code and makes the configuration more readable to the developer, easy to read and write as well. Here everything will be the same only the format of defining the port inside the file is different let’s take a closer look at the declaration and syntax to see below;
e.g.:
server:
port: 8083
As you can see property is are same but eh way of defining and writing the property inside the yml file is different, here we are trying to run the application on 8083, by making few changes.
Examples
1) Simple example to change the port number.
Example:
a) create the project from spring initializer
URL: https://start.spring.io/
b) open application property file and mention the below changes:
code:
server.port=8083
c) Main class be like this with no code change on the other side.
code:
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TradersApplication {
public static void main(String[] args) throws JsonProcessingException {
SpringApplication.run(TradersApplication.class, args);
}
}
d) Run the application and you will see the below output:
Output:
Conclusion
By the use of it, we can run our different applications to a different port, also we can change the default port. it is not difficult and time-consuming to make these changes, because the spring boot framework makes this easy for us, also it is very much readable by the other developers as well.
Recommended Articles
This is a guide to Spring Boot Change Port. Here we discuss Definition, syntax, How to change port in Spring boot? example with code implementation. You may also have a look at the following articles to learn more –