Is it possible to change the port of the embedded Tomcat server in Spring Boot?

Yes, Spring Boot allows us to change the default port of the embedded Tomcat server. By default, Spring Boot applications run on port 8080, but this can be easily customized through configuration files, command-line arguments, environment variables, or programmatically.

Key Points: • The default embedded Tomcat port in Spring Boot is 8080. • The server.port property is used to change the port. • Port configuration can be done through properties files, YAML files, environment variables, or code. • Different ports can be configured for different environments. • Changing the port does not require any code changes in controllers or services.

Method 1: Using application.properties

The most common approach is to configure the port in the application.properties file.

Code Example:

server.port=9090

After startup, the application will be accessible at:

http://localhost:9090

instead of:

http://localhost:8080

Method 2: Using application.yml

Code Example:

server: port: 9090

Spring Boot automatically reads the configuration during startup.

Method 3: Using Command-Line Arguments

The port can be overridden while starting the application.

Code Example:

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

This is useful when deploying the same application to different environments.

Method 4: Using Environment Variables

Code Example:

SERVER_PORT=9090

Spring Boot automatically maps this value to:

server.port

This approach is commonly used in Docker and cloud deployments.

Method 5: Programmatically

Code Example:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collections;

@SpringBootApplication
public class EmployeeApplication {

    public static void main(String[] args) {

        SpringApplication app =
                new SpringApplication(
                        EmployeeApplication.class);

        app.setDefaultProperties(
                Collections.singletonMap(
                        "server.port",
                        "9090"));

        app.run(args);
    }
}

This sets the port during application startup.

How It Works Internally

Application Starts | Read Configuration | Resolve server.port | Configure Embedded Tomcat | Tomcat Starts on Specified Port

Spring Boot reads the port configuration before initializing the embedded server.

Using Random Port

For testing purposes, Spring Boot can use a random available port.

Code Example:

server.port=0

Spring Boot automatically selects an available port at runtime.

Example: Suppose an organization hosts multiple applications on the same server.

Application A:

server.port=8080

Application B:

server.port=9090

Application C:

server.port=7070

Each application can run simultaneously without port conflicts.

Common Use Cases

• Running multiple applications on one machine. • Environment-specific configurations. • Docker container deployments. • Cloud-native applications. • Integration and testing environments.

Benefits

• Easy configuration • No code modifications required • Environment flexibility • Better deployment management • Supports cloud and container platforms

Real-World Example

A company may use:

Development Environment:

server.port=8081

Testing Environment:

server.port=8082

Production Environment:

server.port=8080

Spring Boot loads the appropriate configuration based on the active environment, allowing the same application to run on different ports without changing the source code.

Interview Tip: A concise interview answer is:

"Yes, the embedded Tomcat port can be changed in Spring Boot using the server.port property. This can be configured in application.properties, application.yml, environment variables, command-line arguments, or programmatically. The default port is 8080, but it can be changed to any available port as required."