How to Change the Default Port in Spring Boot

By default, Spring Boot applications run on port 8080. However, you may need to change this port for various reasons, such as avoiding conflicts with other applications. This tutorial will guide you through different methods to change the default port in a Spring Boot application.

Method 1: Using application.properties

The simplest way to change the default port is by setting the server.port property in the application.properties file.

Step 1: Set Up a Spring Boot Project

Use Spring Initializr to create a new project with the following dependencies:

  • Spring Web

Download and unzip the project, then open it in your IDE.

Step 2: Configure application.properties

Set up the application properties to change the default port. This file is located in the src/main/resources directory.

# src/main/resources/application.properties

# Server port configuration
server.port=9090

Explanation:

  • server.port=9090: Changes the server port from the default 8080 to 9090.

Method 2: Using application.yml

You can also change the default port using the application.yml file.

Step 1: Set Up a Spring Boot Project

Use Spring Initializr to create a new project with the following dependencies:

  • Spring Web

Download and unzip the project, then open it in your IDE.

Step 2: Configure application.yml

Set up the application YAML configuration to change the default port. This file is located in the src/main/resources directory.

# src/main/resources/application.yml

server:
  port: 9090

Explanation:

  • server.port: 9090: Changes the server port from the default 8080 to 9090.

Method 3: Using Command Line Arguments

You can override the default port by passing a command line argument when starting the application.

Step 1: Run the Application with a Command Line Argument

Use the following command to run the Spring Boot application with a different port:

./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=9090

Explanation:

  • -Dspring-boot.run.arguments=--server.port=9090: Specifies the port as a command line argument.

Method 4: Programmatically Setting the Port

You can programmatically set the port by configuring a ConfigurableServletWebServerFactory bean.

Step 1: Create a Configuration Class

Create a new configuration class to set the port programmatically.

package com.example.demo.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebServerConfig {

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return factory -> factory.setPort(9090);
    }
}

Explanation:

  • WebServerFactoryCustomizer<ConfigurableWebServerFactory>: Customizes the web server factory to set the port.
  • factory.setPort(9090): Sets the server port to 9090.

Method 5: Using Environment Variables

You can use environment variables to set the port.

Step 1: Set the Environment Variable

Set the SERVER_PORT environment variable before running the application.

For Unix-based systems (Linux, macOS):

export SERVER_PORT=9090
./mvnw spring-boot:run

For Windows:

set SERVER_PORT=9090
./mvnw spring-boot:run

Explanation:

  • SERVER_PORT=9090: Sets the environment variable for the server port to 9090.

Conclusion

In this tutorial, you have learned different methods to change the default port in a Spring Boot application. The methods include:

  • Using application.properties
  • Using application.yml
  • Using command line arguments
  • Programmatically setting the port
  • Using environment variables

By following these steps, you can effectively change the default port of your Spring Boot application to suit your needs.

Comments