Build a complete Backend REST API with Spring Boot & MySQL

Pardeep Kumar
4 min readMay 31, 2021

In this story, we are going to create a complete backend REST API for To-Do App. This is the second part of the course for build a complete application with Spring Boot and Reactjs.

In the Part 1, we have seen how we can design the REST API.

In this section, we are basically going to implement CRUD operations with Spring Boot & OpenAPI Specification.

Specification:-

First, we are going to download the YAML specification. Goto File -> Save as YAML.

  1. Create a new project in your favorite IDE.

After creating a Spring Boot project. We need to change the pom.xml file, to add dependencies & plugins to use the API specification(yml) file.

After changes:-

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.thecodeveal</groupId><artifactId>spring-to-do-app1</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-to-do-app1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><springfox-version>2.7.0</springfox-version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--SpringFox dependencies --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>${springfox-version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>${springfox-version}</version></dependency><dependency><groupId>com.github.joschi.jackson</groupId><artifactId>jackson-datatype-threetenbp</artifactId><version>2.6.4</version></dependency><dependency><groupId>org.openapitools</groupId><artifactId>jackson-databind-nullable</artifactId><version>0.2.1</version></dependency><!-- Bean Validation API support --><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.openapitools</groupId><artifactId>openapi-generator-maven-plugin</artifactId><version>4.2.3</version><executions><execution><goals><goal>generate</goal></goals><configuration><inputSpec>${project.basedir}/src/main/resources/openapi-todo.yaml</inputSpec><generatorName>spring</generatorName><apiPackage>com.thecodereveal.api</apiPackage><modelPackage>com.thecodereveal.model</modelPackage><supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate><configOptions><delegatePattern>false</delegatePattern></configOptions></configuration></execution></executions></plugin></plugins></build></project>

Paste the openapi.yml in your project under path src/main/resources.

Build the project to create open API generated classes using the API specification.

Run as -> Maven build

After the build, you can see the following classes in your project.

Generated classes

Next, we are going to use TaskApi Interface. Create a new Controller and implement all methods inside the TaskApi interface.

Other classes that we created are the following.

  • TaskDetails.java (JPA Entity)
  • TaskMapper (to convert objects)
  • TaskService ( Services Interface)
  • TaskServiceImpl (Service Implementation)
  • TaskRepository (JPA Repository Interface)

After creating all classes, we have to implement all the methods inside TaskAPI Interface. Once the request is handled by the controller we will pass the request to the service layer as shown below.

package com.thecodereveal.app.services;import java.text.ParseException;
import java.util.List;
import java.util.Optional;
import javax.management.RuntimeErrorException;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.thecodereveal.app.entities.TaskDetails;
import com.thecodereveal.app.mapper.TaskMapper;
import com.thecodereveal.app.repository.TaskRepository;
import com.thecodereveal.model.Task;
import com.thecodereveal.model.TaskStatus;
import javassist.NotFoundException;@Service
public class TaskServiceImpl implements TaskService {

@Autowired
TaskMapper taskMapper;

@Autowired
TaskRepository taskRepository;
@Override
@Transactional
public Task createTask(Task task) {
TaskDetails taskDetails = taskMapper.convertToTaskDetails(task);

return taskMapper.convertToTaskDto(taskRepository.save(taskDetails));

}
@Override
@Transactional
public void deleteTask(Long id) throws NotFoundException {
Optional<TaskDetails> taskDetails=taskRepository.findById(id);

if(!taskDetails.isPresent()) {
throw new NotFoundException("No task found with id "+id);
}

TaskDetails task=taskDetails.get();
//task.setTaskStatus(TaskStatus.DELETED);
taskRepository.delete(task);


}
...
}

TaskRepository:-

package com.thecodereveal.app.repository;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.thecodereveal.app.entities.TaskDetails;@Repository
public interface TaskRepository extends JpaRepository<TaskDetails, Long> {
}

Connect MySQL Database:-

spring.datasource.url=jdbc:mysql://host:3306/todoappdb1spring.datasource.username=usernamespring.datasource.password=password#spring.jpa.hibernate.ddl-auto=update

Once you implement all the methods to run the application. Goto Run As -> Spring Boot App.

On browser open URL http://localhost:8080/swagger-ui.html

To Enable Swagger Edit Spring Boot Main class.

After changes restart the application.

Now you can test the operations through swagger. You can found the entire code available on Github.

For more useful content please check my Youtube channel.

Thanks.

--

--