The Tomcat started on port(s): 8080 (http) with context path ”’ log displays the configured tomcat port and context path in Spring boot application. Spring boot allows to configure port for a spring boot application using the server port property in application.properties or application.yaml file. You can change the default port 8080 to any other port number by using the server.port property. Spring boot displays the configured port as Tomcat started on port(s): 8080 (http) with context path ”

The spring boot application is configured with the default tomcat server. The spring boot configures the tomcat server to run on default port 8080. Spring boot provides an option to change the default port in a number of ways.

In this post, we will see the different ways of changing the default server port 8080 and how to Configure Port for a Spring Boot Application



application.properties

The quickest and easiest way to change the default port is to customise the application.properties file. The path for the application.properties file is src/main/resources. If the port is configured in the application.properties file, the default port value 8080 will be overridden by the configured value. The log Tomcat started on port(s): 8080 (http) with context path ”’ displays the current tomcat port and context path in Spring boot application.

src/main/resources/application.properties

server.port=8090
server.servlet.context-path=/yawintutor


application.yml

The other way to customise the default server port is to configure the yaml file. The default spring boot yaml file is the application.yml file. The path to the application.yml file is src/main/resources. If the port is configured in the application.yml file, the default port value 8080 is overridden by the configured value. The log Tomcat started on port(s): 8080 (http) with context path ”’ displays the current tomcat port and context path in Spring boot application.

src/main/resources/application.yml

server:
  port : 8090
  servlet :
    context-path : /yawintutor


Command Line arguments

You should customise the default server port using the command line argument. If the spring boot application is built and packaged in a jar, the default server port can be modified using the command line argument.

java -jar SpringBootApplication.jar --server.port=8090 --server.servlet.context-path=yawintutor

The jar file will use the default port and start the spring boot application with the embedded tomcat server. The java command supports two ways to configure a property. The other way to customise the default port is as follows.

java -Dserver.port=8090 -jar SpringBootApplication.jar


System Properties

The default port can be modified using the system properties of the spring boot application. System properties can be modified using the api system.setProperty() as seen in the example below.

package com.yawintutor;

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

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		System.setProperty("server.port", "8080");
		SpringApplication.run(Application.class, args);
	}

}

Console log

2020-12-31 15:13:06.974  INFO 94230 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-31 15:13:06.976  INFO 94230 --- [           main] com.yawintutor.application.Application   : Started Application in 1.353 seconds (JVM running for 3.969)


Profile based configuration

The spring boot application can be configured according to the environment. The application uses a different configuration when running in a different environment. If the application is running in a developer environment, the application-dev.properties file may be used. If the spring boot application is running in the QA environment, the application-qa.properties will be used. If the application is running in the production environment, the application-prod.properties file will be used.

The default application.properties can be used in all environments. The configuration specific environment will be overridden by the default application.properties file.

application-qa.properties

server.port=8090

application-prod.properties

server.port=80


Random Server Port

You can customise the spring boot application to use some random port. Any time the tomcat starts, a random port is used.

server.port=0

The current running port can be accessed using the code below in the spring boot application.

@Value("${local.server.port}")
private int serverPort;


No Server Port

If you don’t want to use any port to listen, then the server port is configured with value -1. No port will be assigned to the server for the spring boot application. The application could not be invoked.

server.port=-1



Leave a Reply