In this article, you'll learn how to change the default port in Spring Boot. By default, the embedded web server uses port 8080
to start the Spring Boot application. There are several ways available to change that port, as explained below.
Change port using configuration files
The easiest and faster way to change the default port in Spring Boot is by overriding the default value in a configuration file. Spring Boot uses the server.port
configuration property to specify the port.
The following example shows how you can specify a custom port in the application.properties
file:
server.port=8888
Now the server will launch the application on port 8888
. For application.yml
, you need to add the following:
server:
port: 8888
Spring Boot automatically loads the above files if placed in the src/main/resources/
folder.
Change port using system property
You can also set a system property to change the default port of the Spring Boot application. All you need to do is set an environment variable called SERVER_PORT
on the operating system where the server will be started.
For a Unix-based operating system, type the following command to set an environment variable:
export SERVER_PORT=8888
For Windows operating system, you have to use the following command:
setx SERVER_PORT 8888
Change port using command-line arguments
Another way to change the default port in Spring Boot is by using the command-line arguments while launching the application. For example, if you are packaging and running the application as a jar file, you can set the server.port
argument with the Java command:
$ java -jar spring-boot-app.jar --server.port=8888
The above command is equivalent to the following:
$ java -jar -Dserver.port=8888 spring-boot-app.jar
Change port using programmatic configuration
You can change the default port programmatically while starting the application or customizing the embedded server configuration.
To set the port in the main application class while starting the application, use the following code:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.setDefaultProperties(Collections.singletonMap("server.port", "8888"));
application.run(args);
}
}
To customize the embedded server configuration, you have to implement the WebServerFactoryCustomizer
interface as shown below:
@Component
public class PropertiesCustomizer
implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8888);
}
}
Environment-specific ports
If your application is deployed in different environments, you may want to run it on separate ports.
For example, you may want to use port 8888
for development and port 8889
for the production environment for your Spring Boot application.
To achieve this, create a new file called application-dev.properties
for the development environment in the src/main/resources/
folder with the following content:
server.port=8888
Next, create a application-prod.properties
file for the production environment with a different port:
server.port=8889
To activate the desired Spring Boot profile, add the following property to the application.properties
file:
# spring boot active profile - div or prod
spring.profiles.active=dev
That's it. Spring Boot will automatically pick the server port for the currently active profile.
Change to a random port
If you want to run the Spring Boot application on any available random port, just set the server.port=0
property. The embedded web server will look for a free port using OS natives to prevent clashes and assign it to the application.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.