How do you 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

Method 1: Using application.properties

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

server.port=9090

After startup, the application will be accessible at

http://localhost:9090

instead of

http://localhost:8080

Method 2: Using application.yml

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.

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

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

Method 4: Using Environment Variables

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

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.

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

Benefits

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.

Last updated: 11 November 2026

Previous question: What is Spring Boot dependency management?

Next question: What happens if a starter dependency includes conflicting versions of libraries with other dependencies in the project?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions