Deploy Java (Spring boot) application with MS-SQL database on Azure (Part 1)
In this tutorial, we will create and deploy a simple spring boot application based on Java 11 and using MsSql database.
Prerequisites
- Java 11 (or Java 8+)
- Azure CLI
- An Azure account with Free Trial
I — Create the application
First create the spring boot application, to do this, two options are presented:
- From command line using curl:
$> curl https://start.spring.io/starter.tgz -d dependencies=web,data-jpa,sqlserver,h2,lombok,azure-support -d baseDir=azure-spring-mssql-tutorial -d bootVersion=2.4.5.RELEASE -d javaVersion=11 | tar -xzvf –
- Or from https://start.spring.io :
In addition, I’m using H2 database (with local configurations) for local development purpose, and I’m using Lombok to auto generate constructors, setters, getters and much more.
Create the model class with the name Student.
In the Student class :
- Define three variables: id (marked with @Id and @GeneratedValue), name and email (you can add more variables to the class)
- Generate getters, setters, toString method and constructor (Holla, Lombok saves our time and a lot of code lines) by adding the following annotations: @NoArgsConstructor, @ToString, @Setter, @Getter.
- Define the class as an Entity: add the annotation @Entity
Next, is the complete code for the Student class.
Create the Repository interface “StudentRepository”, that extends the CrudRepository interface:
Then, create a service “StudentsService” that use the repository and add the following methods:
- getAllStudents: retrieve the list of students from database.
- getStudentById: retrieve a student by his id
- saveStudent: add new student to the database
- deleteStudent: delete student from database
Finally, create the controller class “StudentsController” and use the “StudentsService” class:
- Mark the class as RestController by using the annotation @RestController
- Add the @RequiredArgsConstructor annotation for auto inject the StudentsService component.
- Define the following methods:
- getAllStudents: It returns the list of all students
- getStudentById: It returns a student by the id specified in the path variable (@PathVariable)
- addStudent: It saves the student specified in the body by the annotation @RequestBody
- deleteStudent: it deletes a specific student by the id specified in the path variable (@PathVariable)
Note that you can use a single package or make differents packages for your code
To run the application in local machine you can add a new file “application-local.yml” and specify the H2 database configurations.
Run the following command to clean and install the project dependencies:
$> mvn clean install
To start the application on local machine, run the command:
$> mvn spring-boot:run -Dspring.profiles.active=local
II — Interacting with the application
To add a new student, you can use postman (or any similar tool) or from command line using curl, and create a new POST request using the following:
- URL: http://localhost:8077/student/
- Content-Type: JSON (application/json)
- And for the body section:
{
"name": "Student name",
"email": "student.email@test.com"
}
After inserting other students, you can open the H2 console and see the students table with all data inserted:
Check the H2 Database from your navigator:
Go to http://localhost:8077/h2-console
- Driver Class: org.h2.Driver
- JDBC URL: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
- User Name: sa
- Password: sa
Select the STUDENT table, and run the SQL query:
SELECT * FROM STUDENT
To GET the list of students, use the following URL: http://localhost:8077/student/
To GET a specific student by his id, use the following URL: http://localhost:8077/student/1
And finally, to DELETE a specific student by id, use the following URL: http://localhost:8077/student/1
III — Deploy the application to Azure Cloud
Check my the next tutorial to see how to deploy the our application to Azure using Azure Portal:
Final step
In the next tutorials we will learn how to deploy the application by using:
- Azure Portal
- Azure CLI
- Github Actions (soon)
- Azure DevOps (soon)
- Terraform (soon)
You can find the source code on my Github: https://github.com/benstitou/azure-spring-mssql-demo