Spring Boot – Change Port

03 Dec, 2021
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. In this article, we will discuss How to change the port number in the Spring Boot.
PortNumber: Port number helps to is various different kind of services for port number 80  always goes to the HTTP services but at a one time only one service at a time. So we have to change the default port number. Let’s discuss how to change the port number.
We can change the port of the Spring Boot in the following ways:
By Adding the configuration in the application properties of the Spring Boot projectBy Implementing the WebServerFactoryCustomizer interface in the component classChanging the configuration of VM options
By Adding the configuration in the application properties of the Spring Boot project
By Implementing the WebServerFactoryCustomizer interface in the component class
Changing the configuration of VM options
First, we need to establish the spring application in our project. Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using a spring initializer and then use an IDE to create a sample GET route. Therefore, to do this, the following steps as depicted below media sequentially as follows:
Go to Spring Initializr
Fill in the details as per the requirements
Click on Generate which will download the starter project
Extract the zip file
Step 1: Go to Spring Initializr
Step 2: Fill in the details as per the requirements. For this application:
Project: Maven
Language: Java
Spring Boot: 2.2.8
Packaging: JAR
Java: 8
Dependencies: Spring Web
Step 3: Click on Generate which will download the starter project.
Step 4: Extract the zip file. Now open a suitable IDE and then go to File->New->Project from existing sources->Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync
Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.
We need to change the port number using the application.properties file in the project structure of the spring application.
application.properties:
server.port=7000
Step 5: Go to src -> main -> java -> com.gfg.Spring.boot.app and run the main application.
Terminal output:
Implementing the WebServerFactoryCustomizer interface in the component class. Create a server-class of the component type so that the IOC container will pick it up during the loading time and implement the interface in them. Go to src -> main -> java -> com.gfg.Spring.boot.app and create a new server.java class
server.java
Java
@Componentpublic class server implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {    @Override    public void customize(ConfigurableWebServerFactory factory) {          // Setting the port number        factory.setPort(1000);    }}
Now run the main application:
Terminal Output:
Go to Run -> Edit Configurations ->SpringBootAppApplication-> VM options
-Dserver.port=8011
After applying the configuration run the main application.
Terminal Output:
Java-Spring-Boot
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.