DEV Community

Nilanchal
Nilanchal

Posted on • Originally published at stacktips.com on

1 1

How to Change Spring Boot Default Port?

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
Enter fullscreen mode Exit fullscreen mode

In the application.yml file, you can find as follows −

server: 
    port: 9090
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

In the application.yml file, you can find it as follows −

server: 
   port: 0

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or by using the equivalent syntax:

java -jar -Dserver.port=8009 spring-boot-app.jar
Enter fullscreen mode Exit fullscreen mode

The post How to Change Spring Boot Default Port? first appeared on Stacktips.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay