When developing a Spring Boot application, sometimes it becomes necessary to change the default HTTP port from 8080 to another port. This may be due to port conflicts, running multiple services simultaneously, or specific deployment requirements. Changing the port can be easily achieved through various configurations. This guide will explore the different methods available for changing the port in a Spring Boot application.

Solution

Changing Port in application.properties

One of the simplest ways to change the port is by specifying it in the application.properties file. This method is straightforward and requires just a single line of configuration.

// src/main/resources/application.properties
server.port=9090

Advantages:

  • Simplicity: Easy to implement with just a single configuration line.
  • Maintainability: Changes are centralized in the properties file, accessible to everyone.

Disadvantages:

  • Environment Specificity: Changing the port in the properties file affects all environments unless profiles are used.

Output:
When you start the Spring Boot application, you will see that it runs on port 9090.

Tomcat started on port(s): 9090 (http) with context path ''
Started Application in 5.412 seconds (JVM running for 6.123)

Changing Port in application.yml

Similar to application.properties, you can also set the port in application.yml.

// src/main/resources/application.yml
server:
  port: 9090

Advantages:

  • Human-Readable: YAML format is often easier to read and write.
  • Organized Structure: Better for managing nested configurations.

Disadvantages:

  • Yaml Parser Support: Requires proper YAML parser configuration in the environment.

Output:
Application runs on the specified port and logs the same as above.

Changing Port Programmatically

You can also change the port programmatically by implementing a WebServerFactoryCustomizer bean.

import org.springframework.boot.web.server.WebServerFactoryCustomizer
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory
import org.springframework.stereotype.Component

@Component
class ServerPortCustomizer : WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    override fun customize(factory: ConfigurableServletWebServerFactory) {
        factory.setPort(9090)
    }
}

Advantages:

  • Flexibility: Programmatically set the port based on dynamic conditions.
  • Centralized: Keeps configuration within the application’s codebase.

Disadvantages:

  • Complexity: Slightly more complex than using configuration files.
  • Hidden Configuration: Configuration is in the code, not visible in the configuration files.

Output:
Similar to configuration file methods, the server will start on port 9090 as indicated by the logs.

Using Command Line Arguments

Sometimes it is convenient to set the port via command line arguments, especially for different environments without changing the code or properties files.

$ java -jar myapp.jar --server.port=9090

Or, if using Maven or Gradle:

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

Advantages:

  • Environment Specific: Easily configurable for different environments during deployment.
  • No Code Changes Required: Does not require any code or properties file changes.

Disadvantages:

  • Complexity in Command Management: Requires proper command management during deployments and testing.

Output:
Logs will show that the application starts on the specified port.

Using Spring Profiles

Sometimes you need different port configurations for various environments like development, production, or testing. This can be achieved using Spring Profiles.

// src/main/resources/application-dev.yml
server:
  port: 8081

// src/main/resources/application-prod.yml
server:
  port: 9090

Set the active profile when running the application:

$ java -jar myapp.jar --spring.profiles.active=dev

Advantages:

  • Environment Specific Configuration: Tailor configurations specifically for different environments.
  • Separation of Concerns: Keeps environment-specific configurations separate and clean.

Disadvantages:

  • Profile Management: Requires careful management of profiles and their respective configurations.

Output:
The port changes based on the active profile.

Similar Topics

  1. How to externalize configuration in Spring Boot?
  2. How to enable/disable SSL in Spring Boot?
  3. How to configure multiple ports in Spring Boot?
  4. How to set Spring active profile programmatically?
  5. How to use environment variables in Spring Boot application?
  6. How to configure embedded web server in Spring Boot?
  7. How to use random unused port in Spring Boot?
  8. How to customize Tomcat in Spring Boot?
  9. How to disable web server in Spring Boot for non-web applications?
  10. How to configure the Actuator endpoints on a different port?
  11. How to control session timeout configuration in Spring Boot?
  12. How to enable HTTP/2 in Spring Boot?
  13. How to deploy Spring Boot application with different environments?
  14. How to run Spring Boot application behind a reverse proxy?
  15. How to configure server compression in Spring Boot?
  16. How to secure Spring Boot application with HTTP Basic Authentication?
  17. How to troubleshoot port conflicts in Spring Boot?
  18. How to perform integration testing with Spring Boot on different ports?
  19. How to customize the embedded Jetty server in Spring Boot?
  20. How to set up Spring Boot application with Docker and dynamic ports?
One thought on “Spring How to Change Port”

Comments are closed.