EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Port
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Port

Spring Boot Port

Introduction to Spring Boot Port

Spring boot port is nothing but using a different port for the different applications, spring boot uses the default port as 8080 for all applications, if we want to change the default port there are multiple ways available for the same. Spring boot provides many ways to change the configuration of the application, the most important configuration is to change the default port of tomcat. If suppose we are running an application on the default port we cannot start another application on this port, so we need to run an application using a different port. We can change the port in spring boot in different ways.

What is Spring Boot Port?

As we know that spring boot is providing the default tomcat server to run our boot application. The boot tomcat server is run on the default port as 8080.

The below example shows that by default spring boot will use port no 8080 to run the application.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)

Spring Boot Port 1

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

We can also change the port in the boot for our application. We can run more than one application by using different ports in the boot.

There are two types of ports that we can change by using the boot application as follows:

  • Custom port
  • Random port

Using the application properties file we need to set the custom port number for the server.port property.

Below example shows that set the custom port for our application in the application.properties file as follows:

Example:

Server.port = 8081

Below example shows set the custom port by using application.yml files as follows:

Example:

server:
port: 8081

To set any random port to the boot tomcat server we are using the random port method to set the port. Using this method we can set any random port to our server.

The below example shows that set the random port for our application in the application.properties file as follows:

Example:

Server.port = 0

Below example shows set the random port by using application.yml files as follows:

Example:

server:
port: 0

In the above example we can see that we are using the server port as 0 means at the time of starting tomcat server it will assign the random port to tomcat server. We can also change the port of the boot tomcat server using the command line, to change the boot tomcat server port by using the command line we need to package our boot application in a jar file and need to launch it from the command-line interface. Using a command-line interface we can configure the port number of the server at the time of project deployment.

Spring Boot Change Port

Boot is providing the default port of the tomcat server. We can change this port by using properties files and interface.

Below are the property files and interfaces which are used to change the default port of the tomcat server in boot.

  • By using the application.yaml file
  • By using the application.properties file
  •  By using command line argument
  • By using the web server factory customizer (WebServerFactoryCustomizer) interface
  • By using EmbeddedServerContainerCustomizer (embedded servlet container customizer) interface

1. By using the application.yaml file.

  • We can change the default port of boot tomcat server by using application.yml file.
  • The below example shows to change the port using application.yml file.

Code:

server:
port: 8081

Output:

Spring Boot Port 2

Spring Boot Port 3

2. By using EmbeddedServletContainerCustomizer interface.

  • We can change the default port of boot tomcat server by using EmbeddedServletContainerCustomizer interface.
  • The below example shows to change the port using EmbeddedServletContainerCustomizer interface.

Code:

@Component
public class EmbeddedPort implements EmbeddedServletContainerCustomizer
{
public void customize(ConfigurableEmbeddedServletContainer container)
{
container.setPort (8099);
}
}

Output:

Spring Boot Port 4

Spring Boot Port 6

3. By using WebServerFactoryCustomizer interface.

  • We can change the default port of boot tomcat server by using WebServerFactoryCustomizer interface.
  • The below example shows to change the port using WebServerFactoryCustomizer interface.

Code:

@Component
public class WebFactory implements WebServerFactoryCustomizer
{
public void customize(ConfigurableWebServerFactory factory)
{
factory.setPort (9001);
}
}

Output:

WebServerFactoryCustomizer interface

Using Application Properties Files

While we are changing the port of the tomcat server in the boot application.properties file is the recommended way. Because it’s very easy as compared to other options.

Below is the step to change the default port of the application in the boot as follows:

1. Create a project template using initializer and give the following name to the project metadata.

  • In the below step we are providing project group name as com.example, artifact name as spring-boot-port, project name as spring-boot-port, package as jar file, and selecting java version as 11.

Group – com.example
Artifact name – spring-boot-port
Name – spring-boot-port
Description – Project of spring-boot-port
Package name – com.example.spring-boot-port
Packaging – Jar
Java – 11
Dependencies – spring web.

Spring Boot Port 8

2. After generating the project extract files and open this project by using tool suite.

Spring Boot Port 9

3. After opening the project using the tool suite check the project and its files.

Spring Boot Port 10

4. Change the custom port using the application.properties file.

  • In this step, we are changing the port of boot tomcat server from default to 8081.
  • We are editing the application.properties file by using the following code.

Code:

server.port=8081

Output:

Change the custom port using application.properties file

5. Run the application.

  • In the below image, we can see that the port is changed from 8080 to 8081. We have defined the custom port as 8081.

Run the application

6. Change the random port in the application.properties file.

  • In this step, we are changing the port of boot tomcat server from default to random.
  • We are editing the application.properties file by using the following code.

Code:

server.port=0

Output:

Change the random port in application.properties file

7. Run the application.

  • In the below image, we can see that the port is changed from 8080 to 53375. We have defined a random port so it will assign the 53375 port no to boot tomcat server.

Run the application

Conclusion

Boot provides many ways to change the configuration of the application, the most important configuration is to change the default port of tomcat. Application.properties file is the recommended way to change the port in boot because it’s very easy compared to other options.

Recommended Articles

This is a guide to Spring Boot Port. Here we discuss the introduction, spring boot change port, and using application properties files. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more