Question
How to Configure the Server Port in Spring Boot
Question
How can I configure the TCP/IP port that a Spring Boot application listens on so that it uses a port other than the default 8080?
Short Answer
By the end of this page, you will understand how Spring Boot chooses its server port and how to change it using application.properties, application.yml, command-line arguments, environment variables, and Java configuration. You will also see when each approach is most useful in real projects.
Concept
Spring Boot applications that include a web server starter, such as spring-boot-starter-web, run with an embedded server like Tomcat. By default, that server listens on port 8080.
The port is simply the network endpoint where your application accepts HTTP requests. If another service is already using 8080, or if your deployment environment requires a different port, you need to override that default.
In Spring Boot, the main setting for this is:
server.port=9090
This matters because real applications often run in different environments:
- Local development may use one port
- Testing may use another port
- Production may be assigned a port by the platform
- Containers and cloud platforms may inject the port through environment variables
Spring Boot makes this easy by supporting multiple configuration sources with a clear precedence order. That means the same app can run with different ports without changing code.
Mental Model
Think of your Spring Boot application like a shop in a building.
- The application is the shop
- The port is the shop's door number
- Clients must know the correct door number to enter
If your app is listening on port 8080, that is like saying the shop is at door 8080. Changing server.port is just changing the door number where the shop accepts visitors.
Spring Boot lets you set that door number in several places, such as config files, environment variables, or startup commands.
Syntax and Examples
The most common way to configure the port is in a configuration file.
Using application.properties
server.port=9090
This tells Spring Boot to start the embedded server on port 9090.
Using application.yml
server:
port: 9090
This does exactly the same thing as the properties example.
Using a command-line argument
java -jar myapp.jar --server.port=9090
This is useful when you want to change the port without editing files.
Using an environment variable
export SERVER_PORT=9090
java -jar myapp.jar
Spring Boot maps SERVER_PORT to server.port automatically.
Using Java code
import org.springframework.boot.SpringApplication;
org.springframework.boot.autoconfigure.SpringBootApplication;
java.util.Collections;
{
{
(DemoApplication.class);
app.setDefaultProperties(Collections.singletonMap(, ));
app.run(args);
}
}
Step by Step Execution
Consider this application.properties file:
server.port=9090
And this application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello";
}
}
Here is what happens step by step:
- Spring Boot starts the application from
main(). - During startup, it loads configuration from known sources, including
application.properties. - It finds
server.port=9090.
Real World Use Cases
Changing the Spring Boot port is common in many situations:
-
Avoiding port conflicts
- If another app is already using
8080, you can run your app on8081or9090.
- If another app is already using
-
Running multiple services locally
- A frontend app might run on
3000 - One Spring Boot API on
8081 - Another on
8082
- A frontend app might run on
-
Environment-specific deployment
- Development:
8081 - Staging:
9080 - Production: assigned by infrastructure
- Development:
-
Containerized applications
- The internal port may be fixed, but environment variables can still be used for flexibility.
-
Platform deployment
- Some cloud platforms provide the port dynamically, and your app must read it from configuration.
-
Integration testing
- Tests may use a random or custom port to avoid clashes with other running services.
Real Codebase Usage
In real projects, developers usually avoid hardcoding ports directly in Java code. Instead, they prefer external configuration so the same application artifact can run in multiple environments.
Common patterns include:
Environment-based configuration
A team may store the default in application.properties:
server.port=8081
Then override it in production with an environment variable:
SERVER_PORT=80
Profile-specific configuration
Spring Boot supports profile files such as:
application-dev.propertiesapplication-test.propertiesapplication-prod.properties
Example:
# application-dev.properties
server.port=8081
# application-prod.properties
server.port=80
Command-line overrides in deployment scripts
Deployment tools often run:
java -jar app.jar --server.port=9091
This is useful for temporary overrides or infrastructure-managed startup scripts.
Common Mistakes
Beginners often run into a few common issues when changing the server port.
1. Putting the property in the wrong file
Spring Boot expects configuration in standard locations such as:
src/main/resources/application.propertiessrc/main/resources/application.yml
Broken example:
# wrong file or wrong folder
server.port=9090
If the file is not loaded, the port will stay at 8080.
2. Using the wrong property name
Broken example:
port=9090
Correct:
server.port=9090
3. YAML indentation errors
Broken example:
server:
port: 9090
Correct:
server:
port: 9090
YAML depends on indentation.
4. Forgetting that another config source overrides the file
Comparisons
Here is a comparison of the main ways to set the Spring Boot server port:
| Method | Example | Best for | Notes |
|---|---|---|---|
application.properties | server.port=9090 | Local defaults and simple apps | Most common and easy to read |
application.yml | server:\n port: 9090 | YAML-based config style | Good when using nested configuration |
| Environment variable | SERVER_PORT=9090 | Containers, CI/CD, cloud | Very common in deployments |
| Command-line argument | --server.port=9090 | Temporary overrides | Usually overrides file values |
Cheat Sheet
Quick reference
Set the port in application.properties
server.port=9090
Set the port in application.yml
server:
port: 9090
Set the port from the command line
java -jar myapp.jar --server.port=9090
Set the port from an environment variable
SERVER_PORT=9090
Use a random available port
server.port=0
Key rule
Spring Boot uses the server.port property to configure the embedded web server port.
Common file location
src/main/resources/application.properties
Typical default
8080
Common problems
FAQ
How do I change the default port in Spring Boot?
Set server.port in application.properties or application.yml.
server.port=9090
What is the default port for a Spring Boot application?
The default port is usually 8080 when running a web application with an embedded server.
Can I set the Spring Boot port using an environment variable?
Yes. Use SERVER_PORT. Spring Boot maps it automatically to server.port.
Can I pass the port when starting the JAR?
Yes.
java -jar app.jar --server.port=9090
Which value wins if I set the port in multiple places?
Spring Boot has a property precedence system. In many common cases, command-line arguments override values from configuration files.
How do I run Spring Boot on a random port?
Set:
server.port=0
The operating system will assign an available port.
Why is my app still starting on port 8080?
Common reasons include:
Mini Project
Description
Build a tiny Spring Boot web application and configure it to run on a custom port instead of the default 8080. This project demonstrates how externalized configuration works in practice and how the port affects the URL used to access your application.
Goal
Create a Spring Boot app that responds with a simple message on a custom port and verify that it runs on that port.
Requirements
- Create a Spring Boot web application with one HTTP endpoint.
- Configure the application to run on port
9090. - Return a simple text response such as
Server is running. - Start the application and access the endpoint in a browser or with
curl.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.