Configuring the TCP/IP Port for Spring Boot Applications

Introduction

Spring Boot simplifies the process of configuring and deploying Java applications by providing sensible defaults. However, there might be scenarios where you need to change these defaults—for instance, when configuring the port on which your application listens for HTTP requests. This tutorial will guide you through various methods to configure the TCP/IP port in a Spring Boot application, helping you to avoid the default port of 8080.

Understanding Default Configuration

By default, Spring Boot applications use port 8080 for their embedded web server (usually Tomcat). While this is convenient during initial development stages, different environments or multiple instances on the same machine might require different ports. Fortunately, configuring the port in a Spring Boot application is straightforward and can be achieved through several methods.

Configuration Methods

1. Using application.properties or application.yml

The simplest way to change the server port is by using configuration files: application.properties or application.yml.

  • application.properties:

    Add the following line to your src/main/resources/application.properties file:

    server.port=8090
    
  • application.yml:

    Alternatively, you can use YAML format by adding these lines in src/main/resources/application.yml:

    server:
      port: 8090
    

These configuration files are loaded at runtime, and specifying the server port here will override the default.

2. Command Line Arguments

Spring Boot supports setting properties directly via command line arguments when you start your application. This is useful for temporary changes or scripting deployments without altering the codebase:

java -jar myapp.jar --server.port=8090

You can also set it through environment variables using JVM system properties:

java -Dserver.port=8090 -jar myapp.jar

3. Environment Variables

Using OS-level environment variables is another flexible approach to configure the port. Here’s how you can do it in UNIX-based systems and Windows:

  • UNIX-based Systems:

    SERVER_PORT=8090 java -jar myapp.jar
    
  • Windows:

    SET SERVER_PORT=8090
    java -jar myapp.jar
    

4. Maven Profiles

For projects using Maven, you can specify the server port as a runtime argument during development:

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

This command starts your application with the specified port without modifying any configuration files.

5. Programmatic Configuration

For more complex setups or when you need to apply additional logic, you can configure the port programmatically:

  • Spring Boot 2.x:

    Create a WebServerFactoryCustomizer bean in a configuration class:

    import org.springframework.boot.web.server.WebServerFactoryCustomizer;
    import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
        @Override
        public void customize(ConfigurableServletWebServerFactory factory) {
            factory.setPort(8042);
        }
    }
    
  • Older Spring Boot Versions:

    Use an EmbeddedServletContainerCustomizer bean:

    import org.springframework.boot.web.server.EmbeddedServletContainerCustomizer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class ServletConfig {
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer() {
            return (container) -> {
                container.setPort(8012);
            };
        }
    }
    

Choosing the Right Method

The method you choose depends on your project’s requirements and deployment environment. For instance:

  • Use configuration files for simple, permanent changes.
  • Opt for command line arguments or environment variables when you need flexibility or are deploying to different environments.
  • Leverage Maven profiles during development stages.
  • Consider programmatic configuration for more complex scenarios.

Conclusion

Configuring the port of a Spring Boot application is an essential skill that can help tailor your application’s behavior in various environments. By understanding and utilizing these methods, you ensure flexibility and control over how your applications are deployed and accessed.

Leave a Reply

Your email address will not be published. Required fields are marked *