Spring Boot is a powerful framework that simplifies the development of new Spring applications through sensible defaults. One of its many features is the embedded server that comes pre-configured, allowing developers to run applications out of the box. However, there might be situations where the default server port (usually 8080) conflicts with other applications or does not meet specific deployment requirements. This guide will walk you through the process of changing the default port in a Spring Boot application, ensuring your application runs smoothly on your desired port.

Advertisement

Spring Boot’s Default Port

Before diving into the customization, it’s essential to understand how Spring Boot configures its embedded server. By default, Spring Boot applications use Tomcat as the embedded server, listening on port 8080. This configuration is flexible and can be easily adjusted through application properties or programmatically.

Method 1: Using the Configuration Files

The simplest way to change the default port is by specifying a new port in the application.properties or application.yml file located in the src/main/resources directory.

For application.properties:


server.port=9090

For application.yml:


server:
  port: 9090

After setting the desired port, restart your application. It should now be accessible on the new port.

For applications deployed across different environments (e.g., development, testing, production), it’s efficient to use Spring profiles to define environment-specific configurations, including server ports.

For application-dev.properties:


server.port=9090

Activate the profile by setting the spring.profiles.active property when starting your application:


java -jar myapplication.jar --spring.profiles.active=dev

This setup allows for flexible and environment-specific configurations, making it easy to manage port settings across various deployment scenarios.

Method 2: Programmatically Setting the Port

If you need more dynamic control over the port configuration, you can set the port programmatically. This approach is useful when the port needs to be determined at runtime based on specific conditions or external configurations.

Example Code:


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

@SpringBootApplication
public class MyApplication {

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

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

This code snippet defines a WebServerFactoryCustomizer bean that customizes the embedded server’s port before the application starts.

Method 3: Using Command Line Arguments

You can also override the default port by specifying a command line argument when starting your application. This method is particularly useful for temporary changes or when deploying the application to different environments that require different ports.


java -jar myapplication.jar --server.port=9090

This command starts the Spring Boot application with the embedded server listening on port 9090.

Conclusion

Changing the default port in a Spring Boot application is straightforward and can be achieved through multiple methods depending on your specific requirements. Whether through application properties, programmatically, using command line arguments, or with environment-specific configurations, Spring Boot offers the flexibility needed to ensure your application runs on the port that best suits your needs. By following this guide, you can customize your Spring Boot application’s port settings to align with your deployment strategies and avoid port conflicts, ensuring smooth application development and deployment processes.

Share.
Leave A Reply


Exit mobile version