Change server port on a Spring Boot application

Today, developing a web-based application is as easy as getting a hand because there are so many tools that we can build a web server right on your laptop or PC. We all know that web-based applications are often allowed to communicate via port 80 or 8080 on most web technologies and web technologies. For that reason, you will have to know how to change the web interface when needed if there are multiple applications running on a real server. In this article, I will present shortly how to change ports for spring boot-based applications.

Depending on how you create and run your Spring Boot application, the way to change the port of running the application is vaguely different. Here are such different ways.

Change the server port from application properties

Regardless of approaches that you used to create the project (i.e. Maven or Gradle), the server port is often changed in application.properties in resources directory with the property server.port.

server.port = 8090

Let’s look at the screenshot below.

Change server port in application properties
Change server port in application properties

Changing this way, you don’t have to specify the server port as long as the application is started. In other words, this approach will affect global on the whole project without considering IDE which you are using for coding.

Change the server port at starting the application from the terminal

In case of changing the server port in place, this approach will override settings of the server port in the application properties. The syntax of specifying the server port is different from a case by case.

Maven

mvn spring-boot:run -Dserver.port=8090

Gradle

SERVER_PORT=8090 gradle bootRun

Or

./gradlew bootRun -Dserver.port=8090

With this piece of code changed:

bootRun {
systemProperties = System.properties
}

If you want to start the application a runnable file such as app.jar, we can do so

java -jar -Dserver.port=8090 spring-boot-example-1.0.jar

java -jar spring-boot-example-1.0.jar server.port=8090

Change the server port when using IntelliJ IDEA

Sometimes, we run the embedded Tomcat server in IntelliJ IDEA directly. The screenshot below shows you how to add the argument as new server port.

Update port using VM option
Update port using VM option

That’s all ways of changing the server port of Spring Boot based application in different situations. Hopefully, my post would be useful for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.