Spring Boot - How to get the running port
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot is a framework widely used in Java environments to simplify the development of new Spring applications. It takes an opinionated view, which means it comes with default configurations that help developers build applications faster. However, despite these default configurations, there are times when understanding and control of the application's running parameters, such as the server port, are essential. This article will explore various methods to determine and manage the port on which a Spring Boot application is running.
Understanding Spring Boot's Default Port
By default, Spring Boot applications start on port 8080. This is because Spring Boot comes with an embedded Tomcat server whose default configuration includes listening on port 8080. However, developers often need to identify or change this port due to port conflicts or specific deployment requirements.
How to Get the Running Port
To get the running port of a Spring Boot application programmatically, you can use different approaches.
Accessing the Port via application.properties or application.yml
Spring Boot allows the configuration of the server port using its properties files.
application.properties:
application.yml:
You can read the specified port from these configuration files. However, if you need to dynamically retrieve the current running port at runtime, you can leverage the EmbeddedWebServer interface.
Using EmbeddedWebServer to Retrieve the Port at Runtime
Spring Boot provides mechanisms for retrieving the actual running port when the port is dynamically set by the operating system (e.g., port 0, which tells the OS to use any available port).
Here's how you can do it:
- Implement a
Beanthat listens when the application starts.
- Use this
Beanwithin your application to access the running port.
Retrieving the Port via Spring's Environment
Another approach is to retrieve the port using Spring's Environment.
This method is effective when the port may change at runtime or be overridden in different environments.
Changing the Default Port
Changing Spring Boot's default port is quite straightforward. This can be done using multiple approaches:
- Properties and YAML Files: As stated earlier, change
server.portinapplication.propertiesorapplication.yml. - Command Line Argument: Use
-Dserver.port=9090when running the JAR file. - Programmatically in a
SpringApplication:
Summary Table
| Method | Description | Example Usage |
application.properties / yml | Set server.port in the configuration files. | server.port=8081
In YAML: Server block |
EmbeddedWebServer Interface | Programmatically access the running port. | Use WebServerApplicationContext |
| Environment Property | Retrieve port through Spring's Environment. | environment.getProperty("local.server.port") |
| Command Line | Override the port during application start. | -Dserver.port=9090 |
| WebServerFactoryCustomizer | Customize the port programmatically on startup. | Override WebServerFactoryCustomizer |
Conclusion
Understanding how to retrieve and configure the running port of a Spring Boot application is essential for various development and deployment scenarios. Whether via configuration files, dynamic runtime values, or programmatically setting values, developers have multiple flexible options to manage server ports in Spring Boot applications. This versatility not only simplifies debugging and development but also streamlines deployment in diverse environments.
Related reading
- Spring Boot - How to log all requests and responses with exceptions in single place?
- Spring Boot - infinite loop service
- Spring Boot - inject map from application.yml
- Spring Boot - Limit on number of connections created
- Spring Boot - Loading Initial Data
- Spring Boot - nesting ConfigurationProperties
- Spring Boot - no log file written logging.file is not respected
- Spring boot - Not a managed type

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.