Introduction:
In this blog, We’ll explore the simplest way to create Spring Boot CRUD RestFul APIs, This blog will cover the topics below,
- Why Spring Boot for APIs?
- What are the CRUD operations?
- What is MySql database?
- Conclusion
Prerequisites:
- JDK
- Intellij idea (or any other preferred IDE like Eclipse)
- MySql
Why Spring Boot for APIs?
As per the current scenario, we know that everyone requires speedy and quality work. As an SDE(software development engineer) we can say that Spring Boot offers a fast way to create applications. We know the Stuff of Spring MVC and all we can say is that in Spring Boot we can reduce the configuration and infrastructure and be able to make more focus on business logic, which is the main part of every application.
Basically, API stands for Application programming interface. With the use of APIs we can easily communicate with our data with the front-end side.And We can easily create an API with Spring Boot because Spring boot provides production ready development.
In simple words REST APIs are nothing but CRUD operations whatever we implement in our codebase.
What is the CRUD Operation?
CRUD Stands for (create , read , update and delete).
WIth using CRUD operation we easily manipulate the data like we can Create the database record , Retrieve the database record , Update the database record and also delete the database record.
What is Mysql Database?
MySql database is a relational database management system. In that we can store our data in tabular format.
First of all we need a project setup so please go to Spring Initializer and set up the project as per our requirements.
![Spring Boot Project | CRUD Restful APIs with Spring Boot Spring Boot Project](https://www.ignek.com/wp-content/uploads/2024/06/Spring-Boot-Project-1024x518.webp)
src
|-- main
| |-- java
| |-- com
| |-- ignek
| |-- Crud_api
| |-- CrudApiApplication.java
| |-- controller
| |-- UserController.java
| |-- model
| |-- User.java
| |-- repository
| |-- UserRepository.java
| |-- service
| |-- UserService.java
| |-- impl
| |-- UserServiceImpl.java
|-- resources
|-- application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/microservices
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Parth@1314
package com.ignek.Crud_api.model;
import jakarta.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "Name")
private String name;
@Column(name = "Email")
private String email;
@Column(name = "Mobile_Number")
private long mobileNumber;
public User() {
}
public User(Long id, String name, String email, long mobileNumber) {
this.id = id;
this.name = name;
this.email = email;
this.mobileNumber = mobileNumber;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(long mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
package com.ignek.Crud_api.repository;
import com.ignek.Crud_api.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository {
}
package com.ignek.Crud_api.service;
import com.ignek.Crud_api.model.User;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public interface UserService {
User createUser(User user);
Optional getUserById(Long id);
List getAllUsers();
User updateUser(Long id, User userDetails);
void deleteUser(Long id);
}
package com.ignek.Crud_api.service;
import com.ignek.Crud_api.model.User;
import com.ignek.Crud_api.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public User createUser(User user) {
return userRepository.save(user);
}
@Override
public Optional getUserById(Long id) {
return userRepository.findById(id);
}
@Override
public List getAllUsers() {
return userRepository.findAll();
}
@Override
public User updateUser(Long id, User userDetails) {
Optional userOptional = userRepository.findById(id);
if (userOptional.isPresent()) {
User user = userOptional.get();
user.setName(userDetails.getName());
user.setEmail(userDetails.getEmail());
user.setMobileNumber(userDetails.getMobileNumber());
return userRepository.save(user);
} else {
throw new RuntimeException("User not found with id " + id);
}
}
@Override
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
package com.ignek.Crud_api.controller;
import com.ignek.Crud_api.model.User;
import com.ignek.Crud_api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
UserService userService;
@PostMapping
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
@GetMapping("/{id}")
public Optional getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@GetMapping
public List getAllUsers() {
return userService.getAllUsers();
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
return userService.updateUser(id, userDetails);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}
CRUD operation is ready now can test them on the postman.
Closure:
CRUD operation is the most important part of Spring Boot Web Development Services will use this blog to learn easily !