Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
151 views

Shashwat Chauhan (Spring Boot Rest API CRUD)

? Diving Deep into API Development with Spring Boot and More ? Hello, amazing LinkedIn community! ? I'm thrilled to share a technical journey that has been months in the making. ? Today, I present to you a comprehensive technical document detailing every facet of my latest project: a Spring Boot CRUD API. Buckle up, because we're delving into the heart of modern development! ? Introduction: We kick off by understanding the essence of this project. Powered by Java 17 and Spring Boot, this AP

Uploaded by

shashwat chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Shashwat Chauhan (Spring Boot Rest API CRUD)

? Diving Deep into API Development with Spring Boot and More ? Hello, amazing LinkedIn community! ? I'm thrilled to share a technical journey that has been months in the making. ? Today, I present to you a comprehensive technical document detailing every facet of my latest project: a Spring Boot CRUD API. Buckle up, because we're delving into the heart of modern development! ? Introduction: We kick off by understanding the essence of this project. Powered by Java 17 and Spring Boot, this AP

Uploaded by

shashwat chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

TECHNICAL DOCUMENT

Spring Boot Rest API CRUD


Using JPA,MYSQL and Postman

Developed by:
Shashwat Chauhan

1|Page
TABLE OF CONTENTS

1. INTRODUCTION…………………………………………………………………………………3

2. OVERVIEW …………………………………………………………………………………..…...3

3. API ENDPOINTS …….…………………………………………………………………………...3

4. DATABASE SCHEMA …………………… …………………………………………………4

5. TECHNOLOGY USED …..……………………………………………………………………5

6. ARCHITECTURE …………………………………….. ……………………………………….5

7. RUN THE APPLICATION……………………………………………………………………….12

8. TESTING THE API USING POSTMAN……………………………… ………………………13

9. PROBLEMS DURING DEVELOPMENT………………………………………………………17

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?

POST /api/employees create new Employee

GET /api/employees retrieve all Employees

GET /api/employees/:id retrieve a Employee


by :empid

PUT /api/employees/:id update a Employee


by :empid

DELETE /api/employees/:id delete a Employee


by :empid

DELETE /api/employees delete all Employees

GET /api/employees?city=[keyword] find all Employee


based on Emp City

GET /api/employees?empAge=[keyword] find all Employee


whose age > empAge
4.
DATABASE SCHEMA-
The API is integrated with a MySQL database. The schema consists of a table that maps to
the resource entity. An example schema definition:

CREATE database IF NOT exists employeedb;

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

The table contains the following columns:

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

• Spring Boot ( dependency : Spring-web,Spring-jpa,mySql)

• Database – MySql

• Created using Maven

• Postman Collaboration platform for API development and testing.

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.

 @GetMapping("/employees/{empid}"): This annotation maps the HTTP GET method


to the "/employees/{empid}" endpoint.

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.

 @PutMapping("/employees/{empid}"): This annotation maps the HTTP PUT method


to the "/employees/{empid}" endpoint.

 public String updateEmployeeById(@PathVariable long empid, @RequestBody


Employee employee): This method updates the details of an employee with the
specified empid in the database.

 @DeleteMapping("/employees/{empid}"): This annotation maps the HTTP DELETE


method to the "/employees/{empid}" endpoint.
 public String deleteEmployeeByEmpId(@PathVariable Long empid): This method
deletes an employee with the specified empid from the database.
 @DeleteMapping("/employees"): This annotation maps the HTTP DELETE method
to the "/employees" endpoint.
 public String deleteAllEmployee(): This method deletes all employees from the
database.
 @GetMapping("/employees/empcity"): This annotation maps the HTTP GET method
to the "/employees/empcity" endpoint.
 public ResponseEntity<Employee>
getEmployeeByempcity(@RequestParam("emp_city") String emp_city): This method
retrieves an employee with the specified emp_city from the database and returns a
ResponseEntity containing the employee if found, or an HTTP NOT_FOUND status if
not found.
 @GetMapping("/employee/employeeGreaterThan"): This annotation maps the HTTP
GET method to the "/employee/employeeGreaterThan" endpoint.
 public ResponseEntity<List<Employee>>
getEmployeeGreaterThan(@RequestParam("emp_age") int emp_age): This method
retrieves all employees with an age greater than the specified emp_age from the
database and returns a ResponseEntity containing the list of employees.
 This is a basic implementation of a RESTful API for managing employees using
Spring Boot and Spring Data JPA. It provides endpoints for creating, retrieving,
updating, and deleting employees in a database.

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.

7. Run the Application

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.

Perform Get by Id operation in Postman


 retrieve a Employee by :empid (/api/employees/:id)

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.

Perform Put operation in Postman


 update a Employee by :empid (/api/employees/:id)

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):

First terminate first process and then re-run the application.

Solution 2 (ii):
Change the port-

And re-run the application.

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

You might also like