Java

REST API with Spring

Build a complete CRUD REST API using Spring Boot.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Build a complete CRUD REST API using Spring Boot. This hands-on tutorial focuses on practical implementation of rest api with spring concepts.

Project: REST API with Spring Boot

We will build a User Management API.

1. Entity

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    // Getters Setters
}

2. Repository

public interface UserRepository extends JpaRepository<User, Long> {
}

3. Service

@Service
public class UserService {
    @Autowired
    private UserRepository repository;
    
    public List<User> getAllUsers() {
        return repository.findAll();
    }
    
    public User createUser(User user) {
        return repository.save(user);
    }
    
    public User getUser(Long id) {
        return repository.findById(id).orElseThrow(() -> new RuntimeException("User not found"));
    }
    
    public void deleteUser(Long id) {
        repository.deleteById(id);
    }
}

4. Controller

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService service;
    
    @GetMapping
    public List<User> getAll() {
        return service.getAllUsers();
    }
    
    @PostMapping
    public User create(@RequestBody User user) {
        return service.createUser(user);
    }
    
    @GetMapping("/{id}")
    public User get(@PathVariable Long id) {
        return service.getUser(id);
    }
    
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        service.deleteUser(id);
    }
}

Testing with Postman

  • POST /api/users: Create JSON body.
  • GET /api/users: List all.
  • GET /api/users/1: Get ID 1.
JAVA PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Spring Boot REST API implementation steps"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

Which annotation maps HTTP GET requests?

@PostMapping
@GetMapping
@RequestMapping