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:
Java SDK (8u112): https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
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.
To verify the installation, you can start a new command window and check the version installed and make sure the commands work.
Test API app locally
I used an existing tutorial app for this blog. Follow the steps from this tutorial - https://spring.io/guides/gs/rest-service/ and clone the code from the repo:
git clone https://github.com/spring-guides/gs-rest-service.git
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:
\deploy\webapps\sbapi.jar
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:
/en-us/azure/app-service-api/app-service-api-java-api-app
The created app setting is configured as:
You can use the API app console to verify the java path:
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"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar "%HOME%\site\wwwroot\webapps\sbapi.jar"">
</httpPlatform>
</system.webServer>
</configuration>
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.
Below is the outcome of the git push:
We can now test the API and see that it works as expected:
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.
Comments
- Anonymous
April 02, 2017
web.config file: Can you explain alittle bit about this file? It makes my app working, so I will be glad to hear about that from you. Thanks!