🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Method 1: Using application.properties
The simplest way to change the default port is by setting the server.port property in the application.properties file.
Step 1: Set Up a 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: Configure application.properties
Set up the application properties to change the default port. This file is located in the src/main/resources directory.
# src/main/resources/application.properties
# Server port configuration
server.port=9090
Explanation:
server.port=9090: Changes the server port from the default 8080 to 9090.
Method 2: Using application.yml
You can also change the default port using the application.yml file.
Step 1: Set Up a 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: Configure application.yml
Set up the application YAML configuration to change the default port. This file is located in the src/main/resources directory.
# src/main/resources/application.yml
server:
port: 9090
Explanation:
server.port: 9090: Changes the server port from the default 8080 to 9090.
Method 3: Using Command Line Arguments
You can override the default port by passing a command line argument when starting the application.
Step 1: Run the Application with a Command Line Argument
Use the following command to run the Spring Boot application with a different port:
./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=9090
Explanation:
-Dspring-boot.run.arguments=--server.port=9090: Specifies the port as a command line argument.
Method 4: Programmatically Setting the Port
You can programmatically set the port by configuring a ConfigurableServletWebServerFactory bean.
Step 1: Create a Configuration Class
Create a new configuration class to set the port programmatically.
package com.example.demo.config;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebServerConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setPort(9090);
}
}
Explanation:
WebServerFactoryCustomizer<ConfigurableWebServerFactory>: Customizes the web server factory to set the port.factory.setPort(9090): Sets the server port to 9090.
Method 5: Using Environment Variables
You can use environment variables to set the port.
Step 1: Set the Environment Variable
Set the SERVER_PORT environment variable before running the application.
For Unix-based systems (Linux, macOS):
export SERVER_PORT=9090
./mvnw spring-boot:run
For Windows:
set SERVER_PORT=9090
./mvnw spring-boot:run
Explanation:
SERVER_PORT=9090: Sets the environment variable for the server port to 9090.
Conclusion
In this tutorial, you have learned different methods to change the default port in a Spring Boot application. The methods include:
- Using
application.properties - Using
application.yml - Using command line arguments
- Programmatically setting the port
- Using environment variables
By following these steps, you can effectively change the default port of your Spring Boot application to suit your needs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment