Introduction
Now that you understand the Spring Boot structure and key annotations, it’s time to build your first real CRUD API.
In this lesson, you will create a simple Users API that allows you to:
- Create a user
- Get all users
- Get user by ID
- Update user
- Delete user
We will use:
- Spring Boot
- JPA (Hibernate)
- H2 in-memory database
No external database needed.
1. Create a New Spring Boot Project
You can generate the project from Spring Initializr with the following dependencies:
- Spring Web
- Spring Data JPA
- H2 Database
Package structure we will use:
com.example.demo
├── controller
├── service
├── repository
└── model
2. Configure H2 In-Memory Database
Add the following to application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
Visit the H2 console after running the app:
http://localhost:8080/h2-console
3. Create the User Entity
model/User.java
package com.example.demo.model;
import jakarta.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
// Getters and setters
}
4. Create the Repository
repository/UserRepository.java
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
This provides built-in CRUD operations.
5. Create the Service Layer
service/UserService.java
package com.example.demo.service;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User create(User user) {
return userRepository.save(user);
}
public List<User> findAll() {
return userRepository.findAll();
}
public User findById(Long id) {
return userRepository.findById(id).orElse(null);
}
public User update(Long id, User newUser) {
User existing = findById(id);
if (existing == null) return null;
existing.setName(newUser.getName());
existing.setEmail(newUser.getEmail());
return userRepository.save(existing);
}
public void delete(Long id) {
userRepository.deleteById(id);
}
}
6. Create the Controller
controller/UserController.java
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@PostMapping
public User create(@RequestBody User user) {
return userService.create(user);
}
@GetMapping
public List<User> getAll() {
return userService.findAll();
}
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
return userService.findById(id);
}
@PutMapping("/{id}")
public User update(@PathVariable Long id, @RequestBody User user) {
return userService.update(id, user);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
userService.delete(id);
}
}
7. Test Your API
Run the project, then use Postman or any REST client.
Create User
POSThttp://localhost:8080/users
Body:
{
"name": "John Doe",
"email": "john@example.com"
}
Get All
GEThttp://localhost:8080/users
Get By ID
GEThttp://localhost:8080/users/1
Update
PUThttp://localhost:8080/users/1
Delete
DELETEhttp://localhost:8080/users/1
Summary
In this tutorial, you learned how to:
- Configure H2 in-memory database
- Create JPA entity
- Build repository, service, and controller layers
- Test CRUD endpoints
This is your first complete REST API in Spring Boot.