The easiest way to configure port for a spring boot application

Spring boot web application runs on a default localhost port 8080 which can be changed or configured in many easy ways.

Using Configuration Files:

We can easily configure the port of the spring boot application using the application.properties file or the application.yml file.

for application.properties we can configure like:

server.port=9292

for application.yml we can configure like:

server:
  port: 9292

Configuration programmatically using Java :

When we start a spring boot project application.java is executed and in that very class we can configure the server port programmatically

Below lines of Codes are used to do the above-given configuration.

application.setDefaultProperties(Collection.SingletonMap("server.port","9292");


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SmartcontactmanagerApplication {

  public static void main(String[] args) {
    SpringApplication.run(SmartcontactmanagerApplication.class, args);
    application.setDefaultProperties(Collection.SingletonMap("server.port","9292");
  }

}

This is how you can configure the server port in two easy ways.

Leave a Reply

Your email address will not be published.