Shashwat Chauhan (Spring Boot Rest API CRUD)
Shashwat Chauhan (Spring Boot Rest API CRUD)
Developed by:
Shashwat Chauhan
1|Page
TABLE OF CONTENTS
1. INTRODUCTION…………………………………………………………………………………3
2. OVERVIEW …………………………………………………………………………………..…...3
10. CONCLUSION…………………………………………………………………………………20
2|Page
1. INTRODUCTION-
This technical document provides a comprehensive overview of the CRUD (Create, Read,
Update, Delete) API developed using Spring Boot, JPA (Java Persistence API), MySQL, and
tested using Postman. It covers the purpose, architecture, endpoints, database schema, and
instructions for testing the API.
2. OVERVIEW-
empid
empname
Employee
empsalary
empage
empcity
3. API ENDPOINTS-
The CRUD API provides the following endpoints:
3|Page
Operation url or API Path What Action will it
do?
1. use employeedb;
2. drop table if exists employee;
3. create table employee (
4. empid bigint not null auto_increment,
5. emp_name varchar(50) default null,
6. emp_salary float default null,
7. emp_age integer default null,
8. emp_city varchar(50) default null,
9. PRIMARY KEY (empid));
10. use employeedb;
11. select * from employee
4|Page
empid: Unique identifier for the Employee.
emp_name: Name of the Employee.
emp_salary: Salary of the Employee.
emp_age: Age of the Employee.
emp_city: City of the Employee.
5. TECHNOLOGY USED-
• Java 17
• Database – MySql
6. ARCHITECTURE-
The CRUD API follows a client-server architecture, where Spring Boot acts as the server,
handling incoming requests and responses. JPA is used for object-relational mapping,
5|Page
enabling seamless interaction with the MySQL database. The architecture consists of the
following components:
Application.propeties:
This is a code snippet that contains configuration properties for a Spring Boot
application using a MySQL database.
spring.datasource.url` is used to specify the URL of the MySQL database with
additional options such as disabling SSL, setting the server timezone, and disabling
the use of legacy datetime code.
`spring.datasource.username` and `spring.datasource.password` are used to provide
the credentials required to connect to the MySQL database.
`spring.jpa.properties.hibernate.dialect` sets the dialect of Hibernate, which is the
ORM (Object Relational Mapping) framework used by default in Spring Boot.
In this case, it is set to 'org.hibernate.dialect.MySQL5Dialect' to match the version of
MySQL being used.
`spring.jpa.properties.hibernate.dialect` sets the dialect of Hibernate, which is the
ORM (Object Relational Mapping) framework used by default in Spring Boot.
In this case, it is set to 'org.hibernate.dialect.MySQL5Dialect' to match the version of
MySQL being used.
`server.port` specifies the port number on which the embedded web server in Spring
Boot will run.
In this case, it is set to 8186.
These configuration properties ensure that Spring Boot can successfully connect to a
MySQL database and configure Hibernate accordingly.
Employee Model:
Employee model contains all the attributes.
6|Page
7|Page
The class is annotated with the @Entity annotation, indicating that it is mapped to
a database table.
The @Table annotation specifies the name of the table, which in this case is
"Employee".
The employee entity has several attributes represented by instance variables,
such as empid (employee id), empname (employee name), empsalary (employee
salary), empage (employee age), and empcity (employee city).
These instance variables are private and are accessed through getter and setter
methods.
The empid field is annotated with @Id and @GeneratedValue annotations.
@Id indicates that it is the primary key for the Employee table, while
@GeneratedValue specifies that its value will be automatically generated using a
specified strategy.
Each attribute also has a corresponding column annotation (@Column) that
specifies the name of the column in the database table.
The class has a constructor that initializes all the attributes and an empty
constructor with no arguments.
The code creates an instance of the Employee class and sets its properties to
default values.
The getEmpid() method returns the Long value that represents the employee's
unique identifier.
The setEmpid() method sets this value to the current value of empid.
The getEmpname() method returns the String value that represents the
employee's name. The getEmpname() method returns the String value that
represents the employee's name.
The setEmpname() method sets this value to empname.
8|Page
The getEmpsalary() method returns the Float value that represents the
employee's salary.
The setEmpsalary() method sets this value to empsalary.
The getEmpage() method returns the int value that represents how many
employees are in this department.
Controller:
Maps incoming requests to corresponding methods.
9|Page
@RestController: This annotation marks the class as a RESTful controller.
@RequestMapping("/api"): This annotation maps the base URL for all the endpoints
defined in this class to "/api".
@Autowired: This annotation is used for automatic dependency injection of the
EmployeeRepository bean.
@PostMapping("/employees"): This annotation maps the HTTP POST method to the
"/employees" endpoint.
public String createNewEmployee(@RequestBody Employee employee): This
method creates a new employee by saving the provided Employee object in the
database.
@GetMapping("/employees"): This annotation maps the HTTP GET method to the
"/employees" endpoint.
public ResponseEntity<List<Employee>> getAllEmployees(): This method retrieves
all employees from the database and returns a ResponseEntity containing the list of
employees.
10 | P a g e
public ResponseEntity<Employee> getEmployeeById(@PathVariable long empid):
This method retrieves an employee with the specified empid from the database and
returns a ResponseEntity containing the employee if found, or an HTTP
NOT_FOUND status if not found.
Repository:
Handles database operations using JPA and communicates with the MySQL
database.
11 | P a g e
import statements: These statements import required classes from various packages.
public interface EmployeeRepository extends JpaRepository<Employee, Long>: This
line defines an interface called EmployeeRepository that extends the JpaRepository
interface. It specifies that it handles Employee entities and uses Long as the type for
the entity's primary key.
Employee findByEmpcity(String emp_city): This method signature defines a custom
query method that retrieves an Employee by their emp_city property. It is
implemented automatically by Spring Data JPA based on the method name.
Optional<List<Employee>> findByEmpageGreaterThan(int emp_age): This method
signature defines another custom query method that retrieves a list of Employee
objects with an emp_age greater than the specified value. It returns an Optional
because there may be no employees matching the criteria. The method is
implemented automatically by Spring Data JPA based on the method name.
This interface acts as a contract for interacting with the Employee entity in the
database. It provides basic CRUD (Create, Read, Update, Delete) operations for the
Employee entity through the inheritance of the JpaRepository. Additionally, it defines
two custom query methods for retrieving employees based on specific criteria.
12 | P a g e
8. Postman
Perform Get operation in Postman
To perform a GET operation in Postman, enter the URL and click on the "Send"
button.
13 | P a g e
Perform Post operation in Postman
To perform a POST operation in Postman, set the request method to POST, enter
the URL, specify the request body (if required), and click on the "Send" button.
14 | P a g e
Perform Delete operation in Postman
delete a Employee by :empid ( /api/employees/:id )
15 | P a g e
Perform Delete operation in Postman
delete all Employees (/api/employees)
16 | P a g e
9. PROBLEMS DURING DEVELOPMENT-
Problems 1:
17 | P a g e
Solution 1:
Correct the url http://localhost:8080/api/employees instead of
http://localhost:8080/api/employee , make sure your path/url should be correct.
Problems 2:
***************************
APPLICATION FAILED TO START
***************************
18 | P a g e
Solution 2 (i):
Solution 2 (ii):
Change the port-
19 | P a g e
10. CONCLUSION-
This technical document has provided a detailed overview of the CRUD API developed using
Spring Boot, JPA, MySQL, and tested using Postman. It covered the architecture, API
endpoints, database schema, and instructions for testing. The document serves as a
comprehensive guide for understanding and utilizing the CRUD API effectively.
20 | P a g e