StackTips

How to Change Spring Boot Default Port?

nilan avtar

Written by

Nilanchala,  3 min read,  3.12K views, updated on Sept. 17, 2023

The Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot.

If you want to change the default port you have several options. But using application.properties option is recommended to overwrite default values.

To override the default port, we need to specify server.port property in application.properties file.

Changing Default Port in Spring Boot

In the application.properties file, we can set a custom port number for the property server.port

server.port = 9090

In the application.yml file, you can find as follows βˆ’

server: 
    port: 9090

Running Spring Boot in Random Port

In the application.properties file, we can set a random port number for the property server.port

server.port = 0

In the application.yml file, you can find it as follows βˆ’

server: 
   port: 0

Note βˆ’ If the server.port number is 0 while starting the Spring Boot application, Tomcat uses the random port number based on the availability.

Using Command-Line Arguments

We can also set the server port using the java command line argument as follows.

java -jar spring-boot-app.jar --server.port=8009

or by using the equivalent syntax:

java -jar -Dserver.port=8009 spring-boot-app.jar

Getting Started with Spring Boot: A Beginner's Guide

This course covers the fundamentals of Spring Boot an you will gain the necessary skills to develop various types of applications using the powerful features of the Spring Boot framework.

>> CHECK OUT THE COURSE
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related posts

Let’s be friends!

πŸ™Œ Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!