Spring Boot – Interview question on port configuration

In this article, we will discuss on how to configure http port for any Spring Boot application.

Spring Boot

As many of the Spring Boot starter aware that, an embedded Tomcat server comes with Spring-Boot-Starter which runs Spring Boot application on default Tomcat port at 8080. So default endpoint URL for Spring Boot application looks like,

http://<server-ip>:<port>/<application-context-path>/<rest-of the-path-starting-from-controller>

But Spring Boot provides flexibility of changing default Tomcat port 8080 to our desired port as per requirement/convenience by declaring parameter-name and its designated value in application.properties file

Before that, we will develop simple Spring Boot application in step-by-step fashion to display current time to the user hitting/requesting endpoint URL

Spring Boot application development

Assumption: all are well-versed with creating Maven-based project in Eclipse IDE supporting Java 1.8 version

Step 1: Include required dependencies inside pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>in.bench.resources</groupId>
	<artifactId>SpringBootDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootDemo</name>
	<description>This is sample Spring Boot demo example for learning purpose</description>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE	</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>
	</dependencies>
</project>

Step 2: Next, start with Spring Boot starter application

SpringMainApplication.java

package in.bench.resources;

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

@SpringBootApplication
public class SpringMainApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringMainApplication.class, args);
	}
}

Step 3: Let’s add controller to expose REST Web Service to return current time in String format

ExposedRestController.java

package in.bench.resources.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExposedRestController {

	// log4j2 - Logger
	Logger logger = LoggerFactory.getLogger(ExposedRestController.class);

	// Endpoint URL -&gt; http://localhost:8080/springboot/time
	@RequestMapping("/time")
	public String exposeService() {

		logger.info("Method_Entered := exposeService");
		return "Today's date is = " + new Date();
	}
}

Step 4: Specify desired application context-path through application.properties file

# root context
server.servlet.context-path=/springboot

Directory Structure

Execution or hitting endpoint URL in browser

  • From below screen-capture, we can say that by-default Spring Boot application runs on 8080 port number
  • And application logic returns current time to the user to be displayed on the browser page
  • And it changes every time new http request sent to Controller or REST Web Service
  • Note: we can cache the result for sometime by writing/adding custom logic to controller

Spring Boot start-up

  • If you look at the below Spring boot start-up console then embedded Tomcat server started at default port number configuration i.e.; 8080
  • With our manually assigned application context-path ‘springboot
  • Click on the below image to see wording clearly

Interview Questions:

1. What are the steps to change default embedded Tomcat server port ?

  • Add one more parameter named ‘server.port‘ in application.properties in addition to already added parameter ‘server.servlet.context-path
  • And assign its value to our desired port number like 8686
  • Note: this port number is different from default tomcat server port 8080

application.properties

# root context and port
server.servlet.context-path=/springboot
server.port=8686
  • Execution and its result as shown below

2. How will you assign random port to Spring Boot application ?

  • Simply assign ‘server.port‘ value to 0 (Zero) in application.properties file
  • But we need to look into Spring Boot start-up to get to know about port number on which this application will execute/run
  • From below screen-capture, we can see that port number is 51591

application.properties

# root context and port
server.servlet.context-path=/springboot
server.port=0
  • Execution and its result as shown below

3. What if we don’t want to put port number itself ?

  • Assign ‘server.port‘ value to 80 in application.properties file
  • As default value for http request served on port number 80
  • So therefore, no need to add port number to endpoint URL

application.properties

# root context and port
server.servlet.context-path=/springboot
server.port=80
  • Execution and its result as shown below
  • From above screen-capture, it can be seen that we aren’t specifying any port number
  • Because http requests are served at port number 80, by default

Additional interesting trivia about context-path or application root-context

  • If we don’t specify context-path in application.properties file then we don’t need to add context-path to our endpoint URL
  • Similarly parameter-name that we define in application.properties file differs based on the Spring Boot version we use in our application
  • We have to use parameter-name ‘server.servlet.context-path‘ for Spring Boot 2.0 version or greater
  • Whereas for Spring Boot 2.0 or less, we have to denote with parameter-name ‘server.contextPath

Happy Coding !!
Happy Learning !!