1. Default Spring Boot port
Spring Boot gives reasonable defaults for its configuration attributes, but also gives the ability to provide custom properties that will suit our needs. By default Spring Boot starts on the port 8080 and we have several options to change that port.
1.1. Configuration file
In order to change starting port from 8080 to 8888 we can override server.port property in configuration with value 8888.
for application.properties we will need to add the following line:
Both application.properties and application.yml files are recognized by Spring Boot automatically when they are located in src/main/resources path.
1.2. System property
Changing Spring Boot port can be also achieved by setting an environment variable SERVER_PORT on the operating system where the server will be started.
- to set an environment variable on Linux use export command:
- to set an environment variable on Windows use setx command:
1.3. Programmatic Configuration
We can change the port programmatically by:
- setting the particular property when starting the application
- customizing the embedded server configuration
1.4. Using a Random Port
Sometimes there is a need to run Spring Boot on a first random available port. To do so you simply have to set server.port=0. Spring Boot prevent clashed using OS natives so don't worry that port will be taken.