Spring Boot Rest Api Crud Example

By | | Updated : 2021-03-16 | Viewed : 3598 times

Spring Boot Rest Api Crud Example

In this quick tutorial, we talk about the rest of API creation for curd operations with MySQL. So be ready for this quick tutorial. Let's create the application and try to understand some of the important components of this application.

Creating a Spring Boot application

To create a spring boot application, you can use Spring Initializer

Or you can use the prepared GitHub link for the same. The repo link in GitHub is Spring-Boot-Curd-Example-App

Required Maven dependencies

Here we are using Mysql and Spring Data JPA. So we need to add the maven dependencies for the same. Please find the below given for the same.

Maven dependencies for CRUD API Tutorial With MySQL, JPA
          <dependency>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-data-jpa</artifactId>
	 </dependency>
	 
	 <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <scope>runtime</scope>
	 </dependency>

Creating Rest Controller Class

Now we will focus to implement different types of the component to create the curd app for curd operations.

Please see below-given cod snippet example for ProductsController.java

ProductsController.java
@RestController
@RequestMapping("/curd-app/api")
public class ProductsController {

    //..

    @GetMapping("/products")
    public ResponseEntity<?> getAllProducts() {
        return productServiceImpl.getAllProducts();
    }

    @PostMapping(value = "/products")
    public ResponseEntity<?> saveProduct(@RequestBody Product product) {
        return productServiceImpl.saveProduct(product);
    }

    @PutMapping(value = "/products")
    public ResponseEntity<?> updateProduct(@RequestBody Product product) {
        return productServiceImpl.updateProduct(product);
    }

    @DeleteMapping(value = "/products")
    public ResponseEntity<?> deleteProduct(@RequestBody Product product) {
        return productServiceImpl.deleteProduct(product);
    }
    @PatchMapping(value = "/products")
    public ResponseEntity<?>patchProduct(@RequestBody Product product) {
        return productServiceImpl.patchProduct(product);
    }
}

Here we used given annotations. those are

@RestController Used to make the particular class as controller class or component for request mapping. So we used one more annotation is @RequestMapping. @RequestMapping is used for mapping request mapping.

@GetMapping This is a shortcut annotation for @RequestMapping(method=HttpMethod.GET) So we can use this for getting resources from the backend.

@PostMapping is a shortcut method for @RequestMapping(method=HttpMethod.Post) So we can save/store the resources by using this annotation.

@PutMapping is a shortcut method for @RequestMapping(method=HttpMethod.Put) So we can update the resources in the backend.

@DeleteMapping is a shortcut method for @RequestMapping(method=HttpMethod.Delete) So we can delete the resource using these annotations.

@PatchMapping is a shortcut method for @RequestMapping(method=HttpMethod.Patch) So we can update the resources in the backend.

Note: put and patch HTTP verbs can be used for updating the resources only. But whenever a partial update is required unlike the whole object then we can use the patch.

Creating Service Class

To write the business logic we need one service class. Here is the below-given code snippet for the same.

ProductServiceImpl.java
@Service
public class ProductServiceImpl {
	 
	@Autowired
	ProductRepository productRepository;
}

Here @Service annotation used with a class that is used for defining the business functionalities. we can consider this is a normal spring bean class where business logic is written here.

@Autowired is used for injecting the dependent bean here.

Creating Entity Class

Here Entity class will be representing the table in Mysql DB. So we can apply different types of curd operations on this DB using Spring Data JPA.

Product.java
@Entity
@Table(name = "product",
	uniqueConstraints = {
	@UniqueConstraint(columnNames = "product_id")
})
@Getter
@Setter
public class Product {

}

@Entity used to specify the entity class for DB operations.

@Table used to specify the table with the annotated entity.

For more information about the annotations please find the below-given link Javax-Persistence-API

Creating the ProductRepository.java

Below given snippet for example repository class. Here we used Spring Data JPA.

ProductRepository.java
@Repository
public interface ProductRepository extends JpaRepository<Product, Long>{
}

Here @Repository will be used to specify the Repository class where all the DB operations are placed here. Here we are extending the JpaRepositroy. So Not Required many implementations for basic curd operations.

Creating SpringBootCurdApplication

The main class where the server will be started easily as embedded tomcat server.

SpringBootCurdApplication.java
@SpringBootApplication
public class SpringBootCurdApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCurdApplication.class, args);
    }

}

Here @SpringBootApplication is annotated in the given class. It is a combined annotation of three other annotations. Those are

@EnableAutoConfiguration: It enables the Spring boot autoconfiguration mechanism.

@ComponentScan: It enables Component scan on the package in Applications So application context easily recognized for beans management.

@Configuration: It allows to register extra beans in the application context.

Execution and Reponses:

So now the application is ready for execution. For the entire code repository in GitHub, you can refer to the link here Spring-Boot-Curd-Example-App

Please find the below-given screenshots for the same.

Spring Boot Rest Api Crud Example
Spring Boot Rest Api Crud Example
Spring Boot Rest Api Crud Example
Spring Boot Rest Api Crud Example
Spring Boot Rest Api Crud Example
Leave A Reply