Change Spring Boot Port

Change Spring Boot Port: Simplified Steps

Learn how to change Spring Boot port with easy steps. Discover multiple methods including application properties, command line, programmatic configuration, and random port assignment.

1. Introduction

Spring Boot applications, by default, run on port 8080. However, there are situations where you might need to change this default port. In this guide, we’ll explore different ways to customize the port on which your Spring Boot application starts, including setting a random port. Whether you’re a beginner or looking to refine your Spring Boot skills, this tutorial has you covered.

2. Change Spring Boot Port Number

2.1 Using application.properties or application.yml

The most common way to change the default port is by modifying the application.properties or application.yml file.

For application.properties:

server.port=9090
application.properties

For application.yml:

server:
  port: 9090
application.yml

Replace 9090 with the port number you want your application to run on. This is a straightforward way to handle a Spring Boot port change.



2.2 Using Command Line Arguments

You can also change the port by passing a command line argument when running your Spring Boot application.

server.port=9090
CMD

Or if you’re using Maven:

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=9090
CMD

This method is particularly useful when you want to override the port number without modifying the configuration files. It’s an easy way to implement a Spring Boot change port command line solution.

2.3 Programmatic Configuration

Programmatic configuration allows setting the port dynamically in a Spring Boot application. You can either set the port directly in the main @SpringBootApplication class using SpringApplication.setDefaultProperties, or customize the server configuration by implementing the WebServerFactoryCustomizer interface and overriding the customize method.

Initially, let’s delve into configuring the property within the main @SpringBootApplication class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collections;

@SpringBootApplication
public class HelloWorldApplication {

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

Alternatively, we can configure the server port by creating a bean that implements the WebServerFactoryCustomizer interface:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

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

}
HelloWorldApplication.class

This approach is ideal for those who need to change the port of embedded Tomcat in Spring Boot dynamically.



3. Order of Evaluation

When changing the port in Spring Boot, the order of evaluation is important to understand. The precedence of port settings in Spring Boot is as follows, from highest to lowest:

  1. Embedded server configuration (via WebServerFactoryCustomizer).
  2. Command line arguments.
  3. application.properties or application.yml files.
  4. Programmatically set properties within HelloWorldApplication main class.

In this order, embedded server configuration set through WebServerFactoryCustomizer has the highest precedence, followed by command line arguments, then properties in configuration files, and finally, programmatically set properties within HelloWorldApplication have the lowest precedence.

4. Using a Random Port

You can configure your application to start on a random available port using Spring Boot’s server.port property.

For a random port assignment, set the port to 0 in application.properties:

server.port=0
application.properties

Or in application.yml:

server:
  port: 0
application.yml

With this configuration, Spring Boot will assign a random available port each time the application starts, ensuring it doesn’t conflict with other services running on the same machine.

Alternatively, if you prefer a specific range of ports, you can specify it like this:

server.port=${random.int(8501,8505)}
application.properties

Or in application.yml:

server:
  port: ${random.int(8501,8505)}
application.yml

In this case, Spring Boot will choose a random port from the specified range (8501 to 8505) on startup. Both approaches are useful for testing and preventing port conflicts.



5. Things to Consider

Here are some important considerations to keep in mind when configuring and managing ports in a Spring Boot application:

  1. Avoid Port Conflicts: Ensure that the port you choose for your Spring Boot application is not already in use by another application or service running on the same machine. Port conflicts can cause your application to fail to start.
  2. Firewall Rules: Update your firewall settings if necessary to allow traffic on the port your Spring Boot application is running on. This is important for external accessibility and network security.
  3. Dynamic Port Allocation: When using a random port, be aware that the assigned port may change each time the application starts. This can impact external systems or configurations that rely on a specific port number.
  4. Port Range Selection: If you choose to specify a port range, ensure that the range is wide enough to accommodate potential port allocations without causing conflicts. Consider factors such as the number of instances running simultaneously and any external integrations that may require specific ports.
  5. Environment-Specific Configurations: Manage port configurations appropriately across different environments (development, testing, production). Use environment-specific configuration files or environment variables to set the port dynamically based on the deployment environment.
  6. Load Balancing and Scaling: If your application is deployed in a clustered or load-balanced environment, consider how port configurations and routing will be handled across multiple instances to ensure seamless communication and scalability.


6. FAQs

Why would I need to change the default port in Spring Boot?

How can I find the random port assigned to my Spring Boot application?

Can I change the port without modifying the configuration files?

Is it possible to change the port dynamically in Spring Boot?

What is the default port for Spring Boot applications?

Can I use a different port for each environment?

7. Conclusion

In conclusion, this guide comprehensively covers various methods to change the default port in Spring Boot applications. Whether through configuration files, command line arguments, or programmatic settings, developers have a range of options to manage port assignments effectively.



8. Learn More

#

Interested in learning more?

Spring Boot Smooth Startup: Performing Basic Checks Before Application Context Creation



Add a Comment

Your email address will not be published.