Create a REST API With Spring Boot

  • IDE or text editor ( Eclipse , Intellij )
  • JDK 1.8+
  • Maven 3+ or Gradle 4+ (We will be relying on Maven for this article)
  • Spring Web — To include Spring MVC and embedded Tomcat into your project
  • Spring Data JPA — Java Persistence API and Hibernate
  • Spring Boot DevTools — Very useful development tools
  • MySQL Driver — JDBC Driver (Can be any DB you’d like to use)
  • Lombok — Java library tool that generates code for minimizing boilerplate code. The library replaces boilerplate code with easy-to-use annotations
<?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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-native.version>0.9.1</spring-native.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>

</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
</pluginRepository>
</pluginRepositories>

</project>

Connect Spring Boot Application With the database

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:port/databasename
spring.datasource.username = username
spring.datasource.password = password
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Creating a Student Model ( Domain Model )

package com.example.demo.model;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="student_tbl")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name; private int age; private String city;}

Creating Repository Classes ( Persistence Layer )

package com.example.demo.repository;import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student,Integer> {
}

Creating a Service ( Business Layer )

package com.example.demo.service;import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public Student addStudent(Student student){
return studentRepository.save(student);
}
public List<Student> getStudents(){
return studentRepository.findAll();
}
public Student getStudentById(int id){
return studentRepository.findById(id).orElse(null);
}
public String deleteStudent(int id){
studentRepository.deleteById(id);
return "Student removed";
}
public Student updateStudent(Student student){
Student existingStudent = studentRepository.findById(student.getId()).orElse(null);
existingStudent.setName(student.getName());
existingStudent.setAge(student.getAge());
existingStudent.setCity(student.getCity());
return studentRepository.save(existingStudent);
}
}

Creating a Controller

package com.example.demo.controller;import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;@CrossOrigin("*")
@RequestMapping(value = "/api/students")
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping
public Student addStudent(@RequestBody Student student){
return studentService.addStudent(student);
}
@GetMapping
public List<Student> getStudents(){
return studentService.getStudents();
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable int id){
return studentService.getStudentById(id);
}
@PutMapping
public Student updateStudent(@RequestBody Student student){
return studentService.updateStudent(student);
}
@DeleteMapping("/{id}")
public String deleteStudent(@PathVariable int id){
return studentService.deleteStudent(id);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Senura Vihan Jayadeva

Senura Vihan Jayadeva

93 Followers

Software Engineering undergraduate of Sri Lanka Institute of Information Technology | Physical Science Undergraduate of University of Sri Jayewardenepura