How to change embebed-tomcat default port using spring boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot starts the embedded Tomcat server on port 8080 by default. Changing that port is usually a configuration task, and the right method depends on whether you want a fixed project default, an environment override, or a value chosen at runtime.
Change the Port in application.properties
The simplest approach is to set server.port in your application configuration.
After restarting the application, the embedded server will listen on port 9090 instead of 8080.
If the project uses YAML, the equivalent is:
This is the best choice when the application should consistently run on the same non-default port in a given environment.
Override the Port from the Command Line
A command-line override is useful for local development, temporary testing, or deployment scripts.
This value overrides the packaged configuration for that specific launch. It is convenient when you need to run multiple Spring Boot services on one machine without editing each project file.
Use Environment Variables
In containerized or cloud environments, environment variables are often cleaner than editing configuration files.
Spring Boot automatically maps SERVER_PORT to server.port. This is a common pattern for Docker, Kubernetes, and platform-managed deployments.
Programmatic Configuration
If the port must be decided from application logic, configure the web server factory in Java.
This is more flexible than plain properties, but it should be used only when configuration alone is not sufficient. Hard-coding infrastructure choices in Java is usually less maintainable than external configuration.
Special Case: Random Port
For tests, it is often useful to let Spring Boot choose a random free port.
With this value, the operating system selects an available port. In integration tests, Spring can inject that port so your test client knows where to connect.
This avoids port collisions on shared CI runners.
How Spring Boot Chooses the Final Value
Spring Boot resolves configuration from multiple sources with a defined precedence order. A command-line argument can override a property file, and an environment variable can override a packaged default. That matters because an application may appear to ignore application.properties when the actual value is coming from somewhere else.
If the server still starts on an unexpected port, inspect:
- command-line arguments
- environment variables
- active profiles
- container or deployment platform configuration
Common Pitfalls
- Changing
application.propertiesand expecting the running process to update without restart does not work. The server port is chosen at startup time. - Forgetting that environment variables or launch arguments override property files can make the configuration appear inconsistent. Check the full configuration chain.
- Hard-coding the port in Java when a property would do adds unnecessary rigidity. Prefer external configuration unless runtime logic is truly required.
- Binding the application to a port already in use causes startup failure. Verify local conflicts before assuming Spring Boot ignored the setting.
- Using a random port in development without discovering the chosen value makes testing awkward. Reserve
server.port=0mainly for automated tests and controlled scenarios.
Summary
- The default embedded Tomcat port in Spring Boot is
8080. - The usual fix is setting
server.portin properties, YAML, or launch arguments. - Environment variables are a clean option for containers and hosted deployments.
- Programmatic customization is available when the port depends on runtime logic.
- If the port seems wrong, check configuration precedence before changing code.
Related reading
- How to change font size in Eclipse for Java text editors?
- How to change root logging level programmatically for logback
- How to change Spring Boot logo to Nyan Cat?
- How to change the decimal separator of DecimalFormat from comma to dot/point?
- How to change the decimal separator of DecimalFormat from comma to dot/point?
- How to change the port of a Spring Boot application using Gradle?
- How to check certificate name and alias in keystore files?
- How to check certificate name and alias in keystore files?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.