How to Configure Your Spring Boot Application to Run on an Alternate Port

Learn how to configure your Spring Boot application to run on an alternate port by modifying the application.properties file or using command-line arguments. Easy steps inside!
How to Configure Your Spring Boot Application to Run on an Alternate Port

How to Run a Spring Boot Application on an Alternate Port

Introduction

Spring Boot is a popular framework for building Java-based applications, known for its simplicity and ease of use. When developing applications, you may want to run your Spring Boot app on a port other than the default port 8080. This could be necessary for various reasons, such as avoiding port conflicts with other applications or adhering to specific networking requirements. In this guide, we will explore different ways to configure your Spring Boot application to run on an alternate port.

Method 1: Application Properties

The simplest way to change the port of your Spring Boot application is by modifying the application.properties file. This file is typically located in the src/main/resources directory of your project. To set a different port, simply add the following line:

server.port=8888

In this example, the application will run on port 8888 instead of the default port 8080. You can replace 8888 with any other available port number. Once you make this change, restart your application to see the effect.

Method 2: YAML Configuration

If you prefer using YAML for configuration, you can achieve the same result by modifying the application.yml file. This file is also located in the src/main/resources directory. Here’s how you can set the server port using YAML:

server:
  port: 8888

As with the properties file, changing the port here will allow your application to run on the specified port after a restart.

Method 3: Command Line Arguments

Another convenient way to specify the port is by using command line arguments when you run your Spring Boot application. You can pass the port as an argument using the following command:

java -jar your-application.jar --server.port=8888

This allows you to run the application on port 8888 without changing any configuration files. This method is particularly useful for temporary changes or when deploying to different environments.

Method 4: Programmatic Configuration

You can also set the server port programmatically within your application. This can be done by defining a custom EmbeddedServletContainerCustomizer bean in your main application class. Here’s a simple example:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;

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

    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        return new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                context.setPath("/myapp");
            }
        };
    }
}

In this example, you can customize the web server settings, including the port, by overriding the appropriate methods.

Conclusion

Changing the port of a Spring Boot application is straightforward and can be accomplished using various methods, including configuration files, command line arguments, and programmatic settings. Choose the method that best fits your development and deployment needs. By running your application on an alternate port, you can avoid conflicts and better manage your application’s environment.