Tuesday, April 21, 2020

How to set/configure port for a Spring Boot application


When we start developing Spring Boot applications, we go on creating more than one applications and try to start them on our local environment.

This is very simple problem but Spring Boot beginners might face it as by default all the applications created through popular IDEs(Spring Tool Suite, intelliJ) have port set to 8080.

Using any of the following ways we can set application to run on any particular port.

According to your convenience you can choose to work with *.properties or *.yml file which resides in /src/main/resources.

If you are using application.yml or application.yaml you can set the port using:

server:
  port: 9090

If you are using application.properties you can set the port using:

server.port = 9090

If you want to use random port every time you start the application you can use:

server:
  port: 0

Or

server.port = 0

Also you might want to make changes to Environment variables temporarily as you may be debugging the application or something similar, then you can add following as your Environment variable or VM argument:

-Dserver.port=9090
Or you can set Environment variable as: 
SERVER_PORT=9090

Any one of the above methods can help you change the port of where application is running, according to me changing *.properties or *.yml will be simplest but you can use any of the above solutions to get going easily.

Hope this post helps you !

No comments:

Post a Comment