Introduction
Spring Boot applications ship with an embedded server, and the default port for them is 8080
. Whether some other service already inhibits this port, or whether you'd like to create a new microservice on a new one - in this guide, we'll take a look at how to configure the port of a Spring Boot application.
application.properties and application.yml
There are two types of properties files typically used in Spring Boot projects - application.properties
and application.yml
.
The application.properties
file follows a simple key-value
format, where each line represents a new key. The application.yml
file follows the YAML format.
Both of these are very human-readable and straightforward and typically, when you start out with a skeleton project, the server.port
is the only setting you'll have.
For application.properties
:
server.port = 8090
For application.yml
:
server:
port: 8090
Note: You can set the port to a random available port, by setting it to 0
. Which is then obtainable through @Value("${local.server.port}")
.
Set Spring Boot Port Programmatically
You can additionally set the port programmatically, since most aspects of customization are ultimately expressible through classes and annotations in Spring Boot.
The WebServerFactoryCustomizer
interface allows us to customize the server factory. The server factory will "consult" any bean of this type before configuring the server itself - so you can set the port, address, error pages, etc. this way as well.
The customize()
method is the only one exposed in the interface, and whatever you don't set in the method is left to the default values. If you just want to change the port, you simply invoke the setPort()
method of the generic factory instance. The default factory is TomcatServletWebServerFactory
:
@Component
public class ChangePort implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setPort(8090);
}
}
For a generic servlet factory, you can use:
@Component
public class ChangePort implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8090);
}
}
Spring Boot automatically picks up beans that implement WebServerFactoryCustomizer
and just by defining it, you can alter the server startup process.
Why would you set it programmatically instead of through a configuration file?
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Changing a configuration file programmatically is a bigger hassle than setting the port programmatically. When deploying applications to remote hosts, you might have to read the proposed port of the application through environment variables or another configuration file and set the application's port based on the input data.
In that case, instead of reading and altering a file, it's much simpler and cleaner to simply read the environment variables, and set the port based on it.
Set Port with CLI
Finally, you can easily change the port of an application when starting it up, via the CLI. You may not be using the CLI to start up the application, in which case, the previous two options are probably better suited for you.
If you're using Maven, when starting up your application, include a JVM argument:
$ mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8090'
Or, if you're using Spring's own CLI, you can pass in command-line arguments separate from Spring's command arguments by adding an additional, blank --
:
$ spring run application_name.groovy -- --server.port=8090
Conclusion
In this short tutorial, we've taken a quick look at three ways to change the default port of a Spring Boot application. You can change the default port by modifying the appropriate properties file, programmatically by implementing a WebServerFactoryCustomizer
or via the CLI.