Spring Boot provides us with many default configurations that allow us to start developing easily. However, on some occasions, we need to modify one of them. In this article, we will see how to change the default port in Spring Boot. Let’s see how changing default port in Spring Boot.
In this post, we will see the different ways to change the port of our application.
Properties file
The fastest and easiest way to change the default port (8080) in Spring Boot is by using the application.yml or application.properties file.
To make this change, we need to specify the port number we want in the server.port property. For example, if we are working with application.properties:
server.port=8090
Or if we are working with application.yml:
server: port: 8090
This property will be loaded on Spring Boot startup and it will be the port to use.
Changing default port in Spring Boot with programmatic configuration
Another way to change the port of a Spring Boot application is by modifying the configuration of our embedded server. To do this, we will use the WebServerFactoryCustomizer class.
To make the port modification programmatically, we will do it in two steps:
@SpringBootApplication public class CustomPortApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(CustomApplication.class); app.setDefaultProperties(Collections .singletonMap("server.port", "8090")); app.run(args); } }
In the previous code, we added the server.port property with its value, and now we will override a class where we indicate the port:
@Component public class ServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8090); } }
In this example, we set the port to 8090, but you can use any valid port number you want. Note that we marked the class with the @Component annotation so that it is picked up by Spring’s component scanning and registered as a bean in the application context.
Command Line
Another practical way to change the port of our Spring Boot application is through the command line using the java command. To do this, we just need to pass the server.port as a parameter. There are two different ways to do it:
java -jar -Dserver.port=8090 refactorizando-port.jar
In this command, we specify the port number (8090 in this case) after the -Dserver.port parameter.
java -jar refactorizando-port.jar --server.port=8090
In this command, we set the server.port system property to the desired port number (8090 in this case) using the –server.port option, and then we run the application as a jar file with the java -jar command.
Note that in both cases, we are setting the port number to a different value than the default port (8090), but you can use any valid port number you want.
know the port on which the Spring Boot application is running
To know the port on which the Spring Boot application is running, you can check the console log output when the application starts up. The port number is typically included in the startup message and will look something like this:
In this example, the application is running on port 8090.
Order of preference
When changes are made to the default configurations, it is important to consider the execution order and priority in Spring Boot.
The order of priority for configuring the embedded server is:
- Embedded server customizations bean
- Command-line arguments
- Properties from application.properties or application.yml
- @SpringBootApplication configuration
This means that any configuration made through the command-line arguments will take precedence over the properties defined in the application.properties or application.yml file. Customizations made through the embedded server customizations bean will take precedence over the @SpringBootApplication configuration.
Conclusion
In this article, changing default port in Spring Boot, we have seen how to modify the default configuration in Spring Boot. We have discussed three different ways to change the default port, although undoubtedly the easiest and most direct way is to modify the properties file.