Quick guide to change the default Spring Boot port

21 feb 2024 2 min di lettura
Quick guide to change the default Spring Boot port
Indice dei contenuti

Introduction

Spring Boot is a powerful framework that simplifies the development of new Spring applications through sensible defaults. One of its many features is the pre-configured built-in server, which allows developers to run applications out of the box. However, there may be situations where the default server port (usually 8080) conflicts with other applications or does not meet specific deployment requirements. This guide will walk you through the process of changing the default port in a Spring Boot application, ensuring that your application runs smoothly on the desired port.

Spring Boot default port

Before diving into customization, it's essential to understand how Spring Boot configures your embedded server. By default, Spring Boot applications use Tomcat as the built-in server, listening on port 8080. This configuration is flexible and can be easily adjusted via application properties or programmatically.

Method 1: Using configuration files

The easiest way to change the default port is to specify a new port in the application.properties or application.yml file located in the src/main/resources directory.

For application.properties:

server.port=9090

For application.yml:

server:
 port: 9090

After setting the desired port, restart the application. It should now be accessible on the new door.

For applications deployed in different environments (e.g., development, test, production), it is efficient to use Spring profiles to define environment-specific configurations, including server ports.

For application-dev.properties:

server.port=9090

Activate the profile by setting the spring.profiles.active property when the application starts:

java -jar myapplication.jar --spring.profiles.active=dev

This configuration allows for flexible, environment-specific configurations, making it easier to manage port settings in various deployment scenarios.

Method 2: Setting the port programmatically

If you need more dynamic control over the port configuration, you can set the port programmatically. This approach is useful when you need to determine the port at runtime based on specific conditions or external configurations.

Example code:

import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.web.server.WebServerFactoryCustomizer;
 import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
 import org.springframework.context.annotation.Bean;

 @SpringBootApplication
 public class MyApplication {

 public static void main(String[] args) {
 SpringApplication.run(MyApplication.class, args);
 }

 @Bean
 public WebServerFactoryCustomizer webServerFactoryCustomizer() {
 return factory -> factory.setPort(9090);
 }
 }

This code snippet defines a WebServerFactoryCustomizer bean that customizes the built-in server port before the application starts.

Method 3: Using command line arguments

You can also override the default port by specifying a command line argument when the application starts. This method is especially useful for temporary changes or when deploying the application to different environments that require different ports.

java -jar myapplication.jar --server.port=9090

This command starts the Spring Boot application with the built-in server listening on port 9090.

Conclusion

Changing the default port in a Spring Boot application is simple and can be achieved through several methods depending on your specific requirements. Whether through application properties, programmatically, using command line arguments, or with environment-specific configurations, Spring Boot provides the flexibility to ensure your application runs on the port best suited to your needs. By following this guide, you can customize your Spring Boot application port settings to align with your deployment strategies and avoid port conflicts, ensuring smooth application development and deployment processes.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.