How to configure port for a Spring Boot application?

In this tutorial I will show you how configure Spring Boot application to run on the different port other than 8080. The port 8080 is the default port for Spring Boot Application which can be changed for running Spring Boot Application on different port.

How to configure port for a Spring Boot application?

--Ads--

How to configure port for a Spring Boot application?

In this tutorial we are going to see several ways to change the default port 8080 to some other port in the Spring Boot Application. Spring Boot comes with many default configurations which makes programming easy and fast. But it is also possible to change these default values by minimal configuration changes or coding. In this tutorial we are going to learn to configure a port for a Spring Boot application.

Spring Boot is a simplest yet powerful framework for building production-ready applications and all these is possible due to the default configuration of various components used in this framework. By default Spring Boot Web Application runs on the port number 8080, but there are many scenarios where you have to change this port to something else. Spring Boot framework makes all these configurations easy and fast. Let?s get started with the tutorial to configure a port for a Spring Boot application.

Spring Boot Web Application Configuration

Steps to configure port for a Spring Boot application

There are many ways you can configure the web application port in the Spring Boot Web application. Let's see all these methods one by one.

Here is the video instruction of Configuring Port for a Spring Boot Web Application

1.Through application.properties configuration

This is the easiest and fastest method of configuring the web server port in Spring Boot Application. By default Spring Boot Web application runs on 8080 port, but by adding the following configuration in application.properties file port number is changed.

server.port=8082

Above configuration changes the Spring Boot Application port number to 8082. Here you can provide any of the free port on your operating system.

2. Configuring port number in application.yml file:

If your project is using application.yml file then you can configure the port using following code:


server:
  port : 8082

Above code changes the web application port to 8082.

3. Environment Specific Configuration

You can use environment specific configuration file in Spring Boot application. Following is the configuration of dev environment (application-dev.properties):

server.port=8082

Following is the configuration of qa environment (application-qa.properties)

server.port=8084

Above configuration file configures qa web application at port number 8084.

4. Programmatic Configuration

There are options for the developers to configure the port of Spring Boot Web application programmatically.


@SpringBootApplication
public class WebappApplication {

	public static void main(String[] args) {
		//SpringApplication.run(WebappApplication.class, args);
		
		SpringApplication app = new SpringApplication(WebappApplication.class);
        app.setDefaultProperties(Collections
          .singletonMap("server.port", "9090"));
        app.run(args);
        
	}

}

Here is the screen shot of the code in Eclipse IDE:

Spring Boot Web Application Configure Port

In the above code we have set the "server.port" value to "9090", which instructs the Spring Boot embedded tomcat to run the application on 9090 port. After making this change you can run the application and you will find your web application running on port 9090.

5. WebServerFactoryCustomizer interface

The next option is use the WebServerFactoryCustomizer interface and configure your application to run on a specific port. Here is the example code:


@Component
class ServerPortCustomizer 
  implements WebServerFactoryCustomizer {
 
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(9091);
    }
}

Above code configuration to run the web application on 9091 port. There are other ways to run the Spring Boot Web application on different port.

6. Using Command-Line Arguments

You can even  provide the port number while running your jar from terminal. Here are the examples:

java -jar SpringBootConfigurePort-0.0.1-SNAPSHOT.jar --server.port=8083

Above command runs the application on 8083 port.

You can even run using following command in terminal:

java -jar -Dserver.port=8083 SpringBootConfigurePort-0.0.1-SNAPSHOT.jar

Above will also run the application on 8083 port.

7. Order of Evaluation

There are several option to run the application on different port. Spring Boot follow a specific order to configure the application. Here is the configuration priority:

1. embedded server configuration

2. command-line arguments

3. property files

0

4. Programmatic configuration in class file annoted with @SpringBootApplication

You should also understand the above order of configuration evaluation by the Spring Boot Framework.

Download Source Code Spring Boot application Port Configuration.

1

Related Tutorials