Getting Started with Spring Boot: Basic REST CRUD Operations

Spring Boot has become one of the most popular frameworks for building Java backend applications. It allows developers to quickly create production-ready applications with minimal setup.

In this post, we’ll build a simple REST API with basic CRUD (Create, Read, Update, Delete) operations using Spring Boot.

What is Spring Boot?

Spring Boot is a framework that simplifies Java application development by:

  • Providing auto-configuration for faster setup.
  • Embedding a web server (Tomcat/Jetty) so no external deployment is needed.
  • Supporting quick creation of REST APIs and microservices.

Project Setup

  1. Go to Spring Initializr.
  2. Add the following dependencies:
    • Spring Web (for REST)
    • Spring Data JPA (for database operations)
    • H2 Database (in-memory DB for simplicity)
  3. Generate and import the project into IntelliJ IDEA or Eclipse

Create Model Class

We’ll create a simple Student entity.

import jakarta.persistence.*;

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private int age;

    // Getters and Setters
}

Step 3: Create Repository

Spring Data JPA provides a ready-to-use JpaRepository.

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {
}

Step 4: Create REST Controller

Now let’s expose CRUD endpoints for Student.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentRepository repository;

    // Create
    @PostMapping
    public Student addStudent(@RequestBody Student student) {
        return repository.save(student);
    }

    // Read All
    @GetMapping
    public List<Student> getAllStudents() {
        return repository.findAll();
    }

    // Read by Id
    @GetMapping("/{id}")
    public Student getStudent(@PathVariable Long id) {
        return repository.findById(id).orElse(null);
    }

    // Update
    @PutMapping("/{id}")
    public Student updateStudent(@PathVariable Long id, @RequestBody Student student) {
        student.setId(id);
        return repository.save(student);
    }

    // Delete
    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable Long id) {
        repository.deleteById(id);
    }
}

Application.yml file content

As a programmer, you should able to configure the application.yml content by taking hint from below YAML file for H2 database

server:
port: 8080

spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
h2:
console:
enabled: true
path: /h2-console

Step 5: Testing the API

You can use Postman or cURL to test the endpoints:

  • Create StudentPOST /students
  • Get All StudentsGET /students
  • Get Student by IDGET /students/{id}
  • Update StudentPUT /students/{id}
  • Delete StudentDELETE /students/{id}

Example request in Postman:

POST /students
{
  "name": "Rahul",
  "age": 22
}

Wrap Up

In this tutorial, we:
✅ Set up a Spring Boot project
✅ Created a Student entity and repository
✅ Implemented CRUD REST APIs
✅ Tested them using Postman

This is the foundation of any Spring Boot REST service. From here, you can add more advanced features like validation, exception handling, DTOs, and security.

👉 Stay tuned for future posts where we’ll dive into those!


✨ If you liked this guide, share it with friends and subscribe for more Spring Boot tutorials.



Leave a Comment

Your email address will not be published. Required fields are marked *

Dare To Dream