Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Change Port

In a Spring Boot application, the default port for the embedded Tomcat server is 8080. If you wish to change the default port (or configure other server settings), you can do so using the application.properties or application.yml file. Here's how you can change the port for your Spring Boot application:

1. Using application.properties:

Open (or create) the application.properties file inside the src/main/resources directory of your project and add:

server.port=8081

Replace 8081 with the desired port number.

2. Using application.yml:

If you're using the application.yml file for configuration, the equivalent configuration would be:

server:
  port: 8081

Again, replace 8081 with the desired port number.

3. Using Command Line:

You can also specify the port when you run your Spring Boot application from the command line by using the --server.port argument:

java -jar your-spring-boot-app.jar --server.port=8081

Or, if you're using the Spring Boot Maven plugin:

mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8081'

4. Using Environment Variables:

Spring Boot properties can also be set via environment variables. For instance, you can set the SERVER_PORT environment variable to the desired port number before running your application.

export SERVER_PORT=8081
java -jar your-spring-boot-app.jar

Remember, command-line properties will always take precedence over property files, so if both are specified, the command-line value will be used.

Once you've made these changes and restarted your application, your Spring Boot application will now run on the new port. Make sure the port you choose is available and not being used by another service on your machine.

  1. Configuring port number in Spring Boot:

    • Configure the port number in application.properties or application.yml.
      # application.yml
      server:
        port: 9090
      
  2. Override default port in Spring Boot:

    • Override the default port programmatically using Java.
      // Java Configuration
      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 MyApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(MyApplication.class, args);
          }
      
          @Bean
          public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
              return factory -> factory.setPort(9090);
          }
      }
      
  3. Command line options to change Spring Boot port:

    • Pass the port as a command-line argument.
      java -jar my-application.jar --server.port=9090
      
  4. Environment properties for setting port in Spring Boot:

    • Use environment properties to set the port.
      # application.properties
      SPRING_APPLICATION_JSON='{"server.port":9090}'
      
  5. Random port assignment in Spring Boot:

    • Assign a random available port.
      # application.yml
      server:
        port: 0
      
  6. External configuration for port in Spring Boot:

    • Use an external configuration file (e.g., application.properties, application.yml).
      # external-configuration.yml
      server:
        port: 9090
      
      java -jar my-application.jar --spring.config.location=classpath:/external-configuration.yml
      
  7. Setting server.port in application.properties in Spring Boot:

    • Define the server.port property in application.properties.
      # application.properties
      server.port=9090
      
  8. Configuring multiple ports in Spring Boot:

    • Configure multiple ports for different protocols.
      # application.yml
      server:
        port: 8080
      management:
        port: 9090