Deploy Spring Boot WAR file on Tomcat in Azure App Service

Hello everyone, Hope you are doing well. In this tutorial, we will learn how to deploy the Spring boot WAR file on tomcat in Azure  App Service.



A little bit of Background

Azure App Service

Azure App Service is an HTTP-based service for hosting web applications, REST APIs, and mobile backends. You can develop in your favourite language, be it .NET, .NET Core, Java, Ruby, Node.js, PHP, or Python. Applications run and scale with ease on both Windows and Linux-based environments. More Info - https://docs.microsoft.com/en-us/azure/app-service/overview

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". More Info - https://spring.io/projects/spring-boot

Apache Tomcat 9

The Apache Tomcat software is an open-source implementation of the Jakarta Servlet, Jakarta Server Pages, Jakarta Expression Language, Jakarta WebSocket, Jakarta Annotations and Jakarta Authentication specifications. These specifications are part of the Jakarta EE platform. 
The Jakarta EE platform is the evolution of the Java EE platform. Tomcat 10 and later implement specifications developed as part of Jakarta EE. Tomcat 9 and earlier implement specifications developed as part of Java EE.

GitHub Actions

GitHub Actions helps you automate your software development workflows from within GitHub. You can deploy workflows in the same place where you store code and collaborate on pull requests and issues. 
In GitHub Actions, a workflow is an automated process that you set up in your GitHub repository. You can build, test, package, release, or deploy any project on GitHub with a workflow. More Info - https://docs.microsoft.com/en-us/azure/developer/github/github-actions


Steps to Deploy Spring Boot WAR file on Tomcat in Azure App Service


Step 1: Creating a simple spring boot web application.

First, open the Spring initializr https://start.spring.io/

Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev and Artifact springboot-greeting-serviceHere I selected the Maven project - language Java - Spring Boot 2.7.1 and add Spring web dependency. The single spring-boot-starter-web dependency transitively pulls in all dependencies cognate to web development.


Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(springboot-greeting-service.zip) file and downloads the project. 
Then, Extract the Zip file.



Step 2: Create a Spring Boot WAR &Import the project on your favourite IDE.

First, we need to package a WAR application instead of a JAR. For this, we'll change pom.xml with the following content:
<packaging>war</packaging>

Next, we'll modify the final WAR file name
<build>
<finalName>springboot-greeting-service</finalName>
...
</build>


Then we'll add the Tomcat dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>


Finally, we'll initialize the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface:
@SpringBootApplication
public class HelloworldApplication
extends SpringBootServletInitializer {

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

@Override
protected SpringApplicationBuilder
configure(SpringApplicationBuilder application) {

return application
.sources(HelloworldApplication.class);
}
}

Import the project File -> Import -> Maven ->Existing Maven Project -> Next -> Browse -> Select the project -> Finish 

When the project imports prosperously, we can optically discern the project directory in the Package Explorer. The following image shows the project directory:




Add a RestController to be able to test, 
@RestController
public class GreetingController {

@GetMapping("greeting")
public String greeting() {

return "Hello, world! Greeting from Azure";
}
}



Step 3: Create a New Repo and Upload Files on GitHub

First, sign in to Github https://github.com/ 

Then, create a new repository, for example, "springboot-greeting-service".



Then, upload the source code from your local machine to the newly created Github repo.




Step 4: Sign in to Azure Portal and create a resource group

Sign in to Azure portal https://portal.azure.com/#home and find "Resource groups" like below.



Then, create a resource group like the one below.


Here the resource group name is "Knowledgefactory". Create a resource group by clicking the "Review + create " button.



Step 5: Create a Web App and Deploy the code from GitHub Repo

Find "App Services",




Then, create a "Web App" like the one below.

Enter/Select necessary information like Resource groupApp nameRuntime stackJava web server stackOSRegion etc... like above. Then click on the "Deployment" button  

Enable GitHub Actions to continuously deploy your app and then enter GitHub account information like above. Here the Repository which I selected is "springboot-greeting-service" which we already created on GitHub.

Next click on the "Review + create" button and you will be taken to "Review + create", as shown in the image below.



Next, click on the "Create" button.


Then, You can see "Deployment is in progress" like the below image.


Once deployment is completed you can see "Your deployment is complete".

Next, Click on "Go to resource" button and you will be taken to the App service Overview page, as shown in the image below. 


Copy the public URL which is marked on the above image and use postman to test the service.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete