How to Change the Default Port in Spring Boot
Spring Boot applications run on port 8080 by default. However, there are several ways to change the default port to suit your needs. In this tutorial, we will explore different methods for changing the default port in a Spring Boot application.
Learn Spring Boot: Complete Spring Boot Tutorial.
Prerequisites
- JDK 17 or later
- Maven or Gradle
- IDE (IntelliJ IDEA, Eclipse, etc.)
Step 1: Set Up a Spring Boot Project
1.1 Create a New Spring Boot Project
Use Spring Initializr to create a new project with the following dependencies:
- Spring Web
Download and unzip the project, then open it in your IDE.
Step 2: Change the Default Port Using application.properties
The most straightforward way to change the default port is by modifying the application.properties
file. This file is located in the src/main/resources
directory.
# src/main/resources/application.properties
server.port=9090
Explanation:
server.port=9090
: Sets the port to 9090. You can change this to any available port number.
Step 3: Change the Default Port Using application.yml
If you prefer using YAML for configuration, you can change the default port by modifying the application.yml
file. Create this file in the src/main/resources
directory if it doesn't exist.
# src/main/resources/application.yml
server:
port: 9090
Explanation:
server.port: 9090
: Sets the port to 9090. You can change this to any available port number.
Step 4: Change the Default Port Programmatically
You can also change the default port programmatically by creating a WebServerFactoryCustomizer
bean. This approach is useful if you want to dynamically set the port based on some condition or environment variable.
package com.example.demo;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServerPortConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setPort(9090);
}
}
Explanation:
@Configuration
: Marks the class as a source of bean definitions.WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
: Customizes the web server factory to change the port.factory.setPort(9090)
: Sets the port to 9090. You can change this to any available port number.
Step 5: Change the Default Port Using Command-Line Arguments
You can override the default port by passing a command-line argument when running the application. This method is useful for temporary changes or for running the application on different ports in different environments without changing the code or configuration files.
./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=9090
Explanation:
-Dspring-boot.run.arguments=--server.port=9090
: Passes the--server.port=9090
argument to the Spring Boot application, overriding the default port. You can change this to any available port number.
Step 6: Change the Default Port Using Environment Variables
You can use environment variables to change the default port. This approach is useful for containerized applications or environments where you can’t easily modify configuration files or command-line arguments.
export SERVER_PORT=9090
./mvnw spring-boot:run
Explanation:
export SERVER_PORT=9090
: Sets theSERVER_PORT
environment variable to 9090../mvnw spring-boot:run
: Runs the Spring Boot application, which will use theSERVER_PORT
environment variable to set the port. You can change this to any available port number.
Step 7: Create a Simple REST Controller for Testing
To verify that the port has been changed, let’s create a simple REST controller.
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Explanation:
@RestController
: Marks the class as a REST controller.@GetMapping("/hello")
: Maps GET requests to/hello
to thehello
method.hello()
: Returns a simple greeting.
Step 8: Running and Testing the Application
8.1 Run the Application
Run the Spring Boot application using any of the methods mentioned above.
8.2 Test the Application
Open your browser and navigate to http://localhost:9090/hello
(assuming you set the port to 9090). You should see the following output:
Hello, World!
Conclusion
In this tutorial, you have learned various methods to change the default port in a Spring Boot application. These methods include modifying application.properties
or application.yml
, using a WebServerFactoryCustomizer
bean, passing command-line arguments, and setting environment variables. This flexibility allows you to configure your application to run on different ports in different environments easily.