Java Spring Boot — Making CRUD Operation Using Spring Data JPA
Hello everyone! In this tutorial I will walk you step by step how to make REST API with CRUD operation using Spring Data JPA. CRUD stands for Create, Read, Update, and Delete, which means we will build an application where we can do data manipulation. Before we start, I wanna tell you that this tutorial is based on the Spring Boot Quick Start course on Java Brains channel on Youtube.
The REST API we will build contains of two types of object, we call it Topic and Course object. The Topic object can contain one or more Course object. We will create API to do CRUD operation to both Topic and Course object.
Add Embedded Database
In this project, I will use embedded database named which apache derby. This is not what we usually use on the real project, but it will be a good exercise to see how we can do CRUD operation in java spring boot. To use it we add the following code to our pom.xml file.
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
Create Model / Entity Class
First we will create the Topic class. This class will be our first model. This class will have three properties, i.e. id, name, and description. We use @Entity annotation to make this class recognized by the spring data JPA so we can do CRUD operation with the object of this class to the database easily. We also use @Id annotation before the initiation of id property to tell the spring data JPA that this property will be the primary key.
package com.mnaufalazwar.springbootquickstart.model;
import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Topic {
@Id
private String id;
private String name;
private String description;
public Topic() {
}
public Topic(String id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}}
Create CrudRepository Class
Then we make TopicRepository class which extends CrudRepository class. CrudRepository is a class from another spring project that already has functions to do CRUD operation so we make TopicRepository class inherit those functions by extending to this class. Take a look we also need to write the class of object we want to do CRUD operation, and the data type of the primary key of that object. So we write <Topic, String>.
package com.mnaufalazwar.springbootquickstart.repository;
import org.springframework.data.repository.CrudRepository;
import com.mnaufalazwar.springbootquickstart.model.Topic;
public interface TopicRepository extends CrudRepository<Topic, String> {
}
Create Service Class
We will use the object of the CrudRepository class in this class.
package com.mnaufalazwar.springbootquickstart.service;
import java.util.ArrayList;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.mnaufalazwar.springbootquickstart.model.Topic;
import com.mnaufalazwar.springbootquickstart.repository.TopicRepository;@Service
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics(){
List<Topic> topics = new ArrayList<>();
topicRepository.findAll().forEach(topics::add);
return topics;
}
public Topic getTopic(String id) {
return topicRepository.findById(id).get();
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(String id, Topic topic) {
topicRepository.save(topic);
}
public void deleteTopic(String id) {
topicRepository.deleteById(id);
}
}
Create Controller Class
Then we create the controller class. This class contains methods that use the object of the Service class to do the CRUD operation.
package com.mnaufalazwar.springbootquickstart.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import com.mnaufalazwar.springbootquickstart.model.Topic;
import com.mnaufalazwar.springbootquickstart.service.TopicService;@RestController
public class TopicController {
@Autowired
private TopicService topicService;
@RequestMapping(value=”/topics”, method=RequestMethod.GET)
public List<Topic> getAllTopics(){
return topicService.getAllTopics();
}
@RequestMapping(value=”/topics/{id}”, method=RequestMethod.GET)
public Topic getTopic(@PathVariable String id) {
return topicService.getTopic(id);
}
@RequestMapping(value=”/topics”, method=RequestMethod.POST)
public void addTopic(@RequestBody Topic topic) {
topicService.addTopic(topic);
}
@RequestMapping(value=”/topics/{id}”, method=RequestMethod.PUT)
public void updateTopic(@PathVariable String id, @RequestBody Topic topic) {
topicService.updateTopic(id, topic);
}
@RequestMapping(value=”/topics/{id}”, method=RequestMethod.DELETE)
public void deleteTopic(@PathVariable String id) {
topicService.deleteTopic(id);
}}
Lets Try The App!
Now let’s run our app. I will use postman to demonstrate what we just made. When we call the first REST API with GET method to http://localhost:8080/topics, we expect to get all the topics object, but since we have no data yet we will just get an empty string as the response.
To add the data, we call another REST API with POST method to http://localhost:8080/topics and we provide it with the request body which is the Topic’s object.
Let’s call the GET method again, now we will see that we have one Topic’s object in our response.
We also can try calling the GET method with the object’s id in the URL to get the specific object.
We also can use the PUT method to edit the data,
and we can see the data is now updated.
The last method we made is the DELETE method which also can we call from http://localhost:8080/topics/java URL. Let’s delete the previous object we put to the database,
and now we can see that the database is already empty again.