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 Change 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 Change Port

Spring Boot Change Port

Definition of Spring Boot Change Port

In spring boot project we have the provision to set the port for our application, the default port for spring boot application is 8080, but we can change it to any number we want by making a few configurations into the application file we have. It may happen that we already have some service which is running on port 8080, so in order to avoid the error we have to change the port of the application, it is very useful. Also, we do not need dependency or very heavy configuration to change the port of our application, sprig boot framework made it very easy for us to change it using the yml or property file we have in class path. We can also have a different port for the different environment this also can be changed and done via the application file we have. So whatever the port is written in the application file it will run the application on that specified port only. In the coming section of the tutorial, we will see how we can change the port by using property and yml file in spring boot project for better understanding and usage.

Syntax:

As we already know that we have to make some configuration or need to set some property in order to change the port number in the spring boot framework, let’s take an closer look at the syntax for this see below;

1) for .properties file:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

e.g.:

server.port= your_port

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,754 ratings)

2) yml file:

e.g. :

server:

port : your_port

As you can see in the above syntax we are trying to use both properties file and yml file, the difference in the syntax for both. Let’s take a closer look at the practice syntax using properties file in spring boot framework for beginners to get a better understanding see below;

e.g.:

server.port= 8082

How to change port in Spring boot?

As we already know that to we can have any number as the port for spring boot application. This is useful in the scenario where we have so many different services running, so in this case, we cannot run each and every service on the same port because a single port can run single application. Let’s take a scenario to understand the problem in a better way, let’s get started

1) Suppose you have three different spring boot applications running on your local machine, but in order to access them, we required the port, to access the URL and get the data.

2) We have created three different projects and the default port for all those 3 will be 8080, once we have completed the coding part now we would require to test the application.

3) Now in my case, my service 1 interact with the service 2, and request for some data. So in order to access the data, we may need the complete URL of the application which contains the port number as well.

4) Now we have service 1 running on port 8080, now in this case we have to change the port number for both my service that is service 2, and service 3.

5) So spring provides us this provision to be implemented using the property file or yml file in the spring boot framework.

6) below see the flow chart to understand the scenario in a better way.

Flow chart:

Spring Boot Change Port

Now we will see how we can change the port by using the property file or yml file in spring boot let’s get started;

1) property file: This is the property file that gets created when we create the spring boot project. Also, this file contains all the configuration which are needed, or the user wants to modify the default once. Spring boot provides us so many properties separated by the ‘.’ dot in a property file, after that, we can provide own value we want. Similarly, we have provided change the port of the application, let get started with the code part;

e.g:

server.port=8082

As you can see in the above code written in the property file, we are changing the port for the application from 8080 to 8082, so when we first open the application property file it is blank and no confit will be there initially, we have to make the changes regarding all the configuration we want for our application. So once we started my server the port will not 8082, and we have to access my application with the new port, suppose if we try to make use of the default port after the application start then it will say cannot be reachable.

2) yml file: This is the same file as the application property file with a different extension. Here we specify the value in the format of key-value pair, this gives more understanding of the code and makes the configuration more readable to the developer, easy to read and write as well. Here everything will be the same only the format of defining the port inside the file is different let’s take a closer look at the declaration and syntax to see below;

e.g.:

server:
port: 8083

As you can see property is are same but eh way of defining and writing the property inside the yml file is different, here we are trying to run the application on 8083, by making few changes.

Examples

1) Simple example to change the port number.

Example:

a) create the project from spring initializer

URL: https://start.spring.io/

b) open application property file and mention the below changes:
code:

server.port=8083

c) Main class be like this with no code change on the other side.

code:

import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TradersApplication {
public static void main(String[] args) throws JsonProcessingException {
SpringApplication.run(TradersApplication.class, args);
}
}

d) Run the application and you will see the below output:

Output:

Spring Boot Change Port 1

Conclusion

By the use of it, we can run our different applications to a different port, also we can change the default port. it is not difficult and time-consuming to make these changes, because the spring boot framework makes this easy for us, also it is very much readable by the other developers as well.

Recommended Articles

This is a guide to Spring Boot Change Port. Here we discuss Definition, syntax, How to change port in Spring boot? example with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Boot Microservices
  2. Spring cloud stream
  3. Spring Boot Profiles
  4. Spring Boot Path Variable
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