Spring Boot – How to Change Default Tomcat Server Port

By default, Spring boot applications starts with embedded tomcat server at port 8080. You can change default tomcat port to any other port, using any one of below technique.

  • Using application.properties or application.yml
  • Implementing EmbeddedServletContainerCustomizer interface
  • Using SpringApplication class
  • Change port directly through command line

Please follow this article carefully, as this is the first spring boot application I am going to explain each and every step with screenshot. In the next tutorial we will discuss about SPRING INITIALIZR which is a cool feature provided by spring.io to bootstrap Spring Boot Project. We are going to use SPRING INITIALIZR extensively for all examples. But for better understanding of Spring Boot configuration this tutorial is very important.

Tools and Technologies used for this application :

  • STS
  • Spring Boot 1.5.4.RELEASE
  • Java 1.8
  • Maven 3.x

Let's Start development of the project step by step. First we need to create Maven project in STS. Follow the steps mentioned below to create Maven Project.

Step 1 : Create Maven Project

Create a maven project for our Spring Boot application. Open STS (Spring Tool Suite) then click on File -> New -> Maven Project as shown below.

Spring Boot Hello World


Step 2 : Select simple project

Select the checkbox 'Create a simple project (skip archetype selection)' and click on 'Next' button.

Spring Boot Hello World


Step 3 : Provide basic details of the project

Provide Group Id , Artifact Id , Name and Description and click on Finish button. Here 'Artifact Id is name of the project and 'Group Id' is the name of the base package.

Spring Boot Hello World

After clicking on 'Finish' button you can see a Maven project created in your STS as shown below.

Spring Boot Hello World


Step 4 : Change the compiler version

As you can see in maven project structure, the default java compiler version ( i.e. source and target setting ) is 1.5. To change the default compiler version to 1.8, add the maven compiler plugin to pom.xml.

 <build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
		</plugin>
	</plugins>
</build>
						

Step 5 : Add Spring Boot related configuration in the pom.xml

Open your pom.xml file and add Spring boot related configuration in that file as given below.

<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>com.jwt.spring.boot.hello</groupId>
	<artifactId>HelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>HelloWorld</name>
	<description>Hello World Project using Spring Boot</description>

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

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>

				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
						

Spring Boot provides the various "Starters" for building Spring Boot based java application. The spring-boot-starter-parent is a special type of starter, which is used as a parent in pom.xml file of any kind of Spring Boot application.

All Spring Boot projects typically use spring-boot-starter-parent as the parent in pom.xml.In most of the cases, your maven project POM will simply inherit from the spring-boot-starter-parent project. The spring-boot-starter-parent

  • provides useful Maven defaults.
  • provides dependency-management section so that you can omit version tags for dependencies you need for your own project.
  • The spring-boot-starter-parent provides the common configuration such default java compiler level, plugin configuration, UTF-8 source encoding, dependency management etc.

There are various 'starters' which will provide dependencies that we are likely to need when developing a specific type of application. Since we are developing a web application, we will add a spring-boot-starter-web dependency.In our pom.xml you can see the starter declaration at line no 16-21.

There are various starters such as spring-boot-starter-web for building Spring web application, spring-boot-starter-web-services for building Spring web service application.

Step 6 : Update the Maven Project

After changes in pom.xml file, update the maven project. After updating the project compiler version and all the dependency will be updated. To update maven project right click on maven-project → Maven → Update Project as shown below.

Spring Boot Hello World


Once update is done just observe the directory structure of the project, it will create a new folder named "Maven Dependencies" which contains all supporting .jars to run the Spring Boot application and the Java version also changed to 1.8.

Spring Boot Hello World



Step 7: Create Controller Class

To be able to handle web request, our application should have controllers. Here is a sample controller HelloWorldController that handles the request to the path "/hello". Create a simple rest controller class inside "com.jwt.spring.boot.controller" package as follows.

package com.jwt.spring.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

	@RequestMapping("/hello")
	public String sayHello() {
		return "Hello World Spring Boot!!";
	}

}

						

Step 8: Main class

To bootstrap spring boot application, create a main class annotated with @SpringBootApplication annotation.

@SpringBootApplication initializes Spring(Component Scan) and Spring Boot (Auto Configuration).

package com.jwt.spring.boot;

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

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

}

						

Then @SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.


Final Project Structure

Final Structure of the project is given below. Please review tour project structure before running the app.

Spring Boot Hello World



Step 9: Running the Application

Finally, we can now run our simple spring boot web application. To run, open the class "MainApp.java" then right click and run or debug to debug the app. This will display the spring boot banner and logs in the console as shown below.

Spring Boot Hello World


Now, enter the http://localhost:8080/hello in browser's address bar and see the output.

Spring Boot Hello World



Download Code


In the next article I will discuss about Spring Boot Project with Spring Initializr.


References:

Spring Boot Official Documentation

comments powered by Disqus