Deploy Java 8 Spring Boot API to Azure App Service
Spring is a popular framework for Java based application development. Spring Boot makes Spring based application development easier by automate and encapsulate some complexity such as dependency and configuration management.
Recently, I have a customer asking how to deploy a Spring Boot based Java API app to Azure. While there are several articles on similar topics, I cannot point the customer to a complete process. This blog serves the purpose.
Local environment setup
I need to have a local environment to test and package the Spring Boot API application before I can deploy it to Azure. I used Windows 2012R2 server for this walk through.
First, I installed the latest versions of Java SDK and Maven tool from the links below:
Install Maven (3.3.9): https://maven.apache.org/download.cgi
There are 2 additional steps to complete the setup:
1. Make sure both Java and Maven path are in PATH variable.
2. Create a new environment variable JAVA_HOME so Maven will work.
Test API app locally
I used an existing tutorial app for this blog. Follow the steps from this tutorial - http://spring.io/guides/gs/rest-service/ and clone the code from the repo:
Build and test from local machine:
After validating the application, I created a jar package with the command below. This jar file will be deployed to Azure:
mvnw clean package
The generated jar file is renamed to sbapi.jar and copied to this folder structure:
Create and Configure Azure API App
I can now move to Azure to create an Azure API App to host the greeting API app. I followed the same steps as in:
In addition to sbapi.jar file, we also need to add a web.config file to the deploy folder to tell API App Java home and command line to run the jar file.
Create a web.config file to the \deploy root (\deploy\web.config) and add the following content (modified from /en-us/azure/app-service-web/web-sites-java-custom-upload):
<?xml version="1.0" encoding="UTF-8"?>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar "%HOME%\site\wwwroot\webapps\sbapi.jar"">
Follow the steps described at /en-us/azure/app-service-api/app-service-api-java-api-app to deploy the sbapi.jar file to the created Azure API Web site.
I hope this blog showed you that you can easily deploy a Spring Boot based Java 8 API application to Azure API App.
This blog is the result of collaboration with my teammate Anand Raman.