Configuring the Server Port in Spring Boot
Fast recall
Quick revision
Core answer
server.port=8081 in application.properties or application.yml. But Spring Boot's property binding accepts the same value from multiple sources with a strict precedence: command-line arguments (--server.port=8082) > JVM system properties (-Dserver.port=8082) > OS environment variables (SERVER_PORT=8082) > profile-specific property files > `application.properties` > defaults. Setting server.port=0 tells Spring Boot to pick a random free port at startup — ideal for integration tests using @SpringBootTest(webEnvironment = RANDOM_PORT) with @LocalServerPort injection to avoid port conflicts in parallel CI runs.Whether you understand Spring Boot's externalized configuration model and its precedence order — not just the single `server.port` property
Only mentioning `application.properties`; forgetting environment variables and command-line overrides; hardcoding the port for production rather than using `${PORT}` substitution
Explain `server.port=0` for integration tests with `@LocalServerPort`, and mention relaxed binding — `SERVER_PORT` env var automatically maps to `server.port` property
Interview-ready guide
Interview answer
The simplest way to configure the server port is server.port=8081 in application.properties or application.yml. But the more interesting answer is Spring Boot's externalized configuration model.
The same property can come from multiple sources with a strict precedence.
- From highest to lowest: command-line arguments, JVM system properties, OS environment variables, profile-specific property files, `application.properties`, and finally defaults.
- So
java -jar app.jar --server.port=8082beats the env var, which beats the file.
Relaxed binding is a nice detail — Spring maps SERVER_PORT env var directly to server.port without any code.
Setting `server.port=0` is a useful trick for integration tests — Spring picks a free port at startup, and you inject it with @LocalServerPort. This avoids port conflicts when tests run in parallel in CI.
For production, I never hardcode the port in the packaged JAR. I use ${PORT:8080} so the artifact respects the platform's PORT env var — that's how Heroku, Cloud Run, and Cloud Foundry all work.
The broader point: externalized configuration is the reason the same JAR is deployable across dev, staging, and production without rebuilding. That's the 12-Factor App principle and the real answer to why Spring Boot structures properties this way.
Configuring the Server Port in Spring Boot
Direct Answer
The standard way is server.port=8081 in application.properties or application.yml. But Spring Boot's property binding accepts the same value from multiple sources with a strict precedence: command-line arguments (--server.port=8082) > JVM system properties (-Dserver.port=8082) > OS environment variables (SERVER_PORT=8082) > profile-specific property files > application.properties > defaults. Setting server.port=0 tells Spring Boot to pick a random free port at startup — ideal for integration tests using @SpringBootTest(webEnvironment = RANDOM_PORT) with @LocalServerPort injection to avoid port conflicts in parallel CI runs.
Common Mistakes & Pitfalls
Only mentioning application.properties; forgetting environment variables and command-line overrides; hardcoding the port for production rather than using ${PORT} substitution
Check your understanding
Answer these without looking back at the article.
- What does `server.port=0` actually do at the servlet container level?
- How does relaxed binding decide that `SERVER_PORT` maps to `server.port`?
- How do you configure different ports for the main app and the Actuator management endpoints?
- What's the precedence between `application-prod.properties` inside the JAR and `application.properties` next to the JAR?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.