This example assumes you have previously completed the following examples:
This example will create a Spring Boot application which has a couple of endpoints for creating and retrieving employee details.
-
In the Azure Portal, search for SQL database
-
Click on SQL Database and in the Create SQL Database page
i. Provide a unique DB name
ii. Under Server, click new upon which a new page will pop up called as Create SQL Database Server
iii. Provide a unique Server name and select the location in the dropdown (eg -> (US) East US)
iv. Under the Authentication -> Authentication method, choose Use SQL authentication (could be Use Microsoft Entra-only authentication as well)
v. In case of sql authentication, provide below details
-
Server admin login
-
Password
-
-
Leave the other options as such and choose Development as the workload environment
-
Go to the networking tab
-
Under the network connectivity, select Connectivity method as Public endpoint
-
In the firewall rules, Choose "yes" for Allow Azure services and resources to access this server and Add current client IP address
-
Then click on review and create
-
This will create a database instance and will get a message "Deployment is completed" and there would be a button "Go to resource". Upon clicking on that, it will show the sql instance
-
In that, copy the Connection strings (JDBC) and put in application.yml file
-
Provide the username and password (given in the SQL authentication page) in yml file
-
Then go to Set Server Firewall in the SQL instance page and then click on "Add your clinet IPV4 address"
-
Click on Query editor (preview) in the SQL instance page
-
Enter the username and password (provided as part of sql authentication step in the SQL Server Page)
-
On the other side, create a Spring Boot application with help of https://start.spring.io
-
Open the project in the IDE and then create the below classes
i. An Employee entity class
@Entity
@Table
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
@Id
@GeneratedValue
private int id;
private String name;
private String department;
private long salary;
}
ii. An Employee repository class for the DB connection
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
}
iii. A Rest Controller with endpoints to create and retrieve an employee object
@SpringBootApplication
@RestController
public class SqlApplication {
@Autowired
private EmployeeRepository repository;
@PostMapping("/employee")
public Employee createEmployee(@RequestBody Employee employee) {
return repository.save(employee);
}
@GetMapping("/employees")
public List<Employee> retrieveEmployees() {
return repository.findAll();
}
public static void main(String[] args) {
SpringApplication.run(SqlApplication.class, args);
}
}
iv. An application.yml file with below properties
spring:
datasource:
url: <jbdc URL obtained from Step 9 in Create an Azure SQL DB>
username: <username given in SQL Server page>
password: <password given in SQL Server page>
jpa:
show-sql: true
hibernate:
ddl-auto: update
dialect : org.hibernate.dialect.SQLServer2012Dialect
server:
port: 8080
-
Start the application in the code editor
-
Then head over to postman client to test the endpoints
-
Select the post request and hit the URL : http://localhost:8080/employee and provide a sample json as below in the request body
{
"name":"Shiva",
"department":"Chemical",
"salary":25000
}
-
If we refresh the Query editor now in Azure SQL DB, dbo.employee table should be listed under the Tables
-
Then enter the query as select * from employee would fetch all the employees that were created as part of the postman requests.
Follow the below steps to create an Azure App Service and deploy the Spring Boot application,
- Click on Resource groups as in the below image
Resource group - It is a container that holds the resources for any Azure solution and is a prerequisite for creating anything on Azure.
Click on create and provide a resource-group and then it would be created. 2. Now, click on App Services -> Create -> Web App
1. Provide the web app name, java version (17/21), operating system (Windows/Linux), region (US/India) etc.
2. Then click on "Review+Create" at the bottom.
This is how the newly created app service would look like
- Execute the below git commands and push local code to github branch
git remote add origin <repository_url> (repository url indicates the newly created github repository)
git add . (to add all the changes done in local)
git commit -m "First commit" (does the commit with a message)
git push -u origin master (push all the changes to github repository)
-
In the source dropdown, select Github and Authorise which would ask for the username and password.
-
After that, select the organization, repository, branch as well in the dropdown.
-
You can click on the "Preview file" to see what steps would be performed as part of the build and deploy process
-
Click on save
-
The build will start & complete after which the deployment will start and succeed.
Get the URL (should look something like https://springboot-azuresql.azurewebsites.net/employees) from azure app service and append "employees" at the end to see the list of employees that were created earlier.
Do NOT forget to remove the resources once you have completed the entire flow.