0Pricing
Java Academy · Lesson

CRUD Service Layers

Implement a service layer that provides CRUD operations for entities.

CRUD Service Layers is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Service layer sits between controller and repository. It centralizes business logic.

Code: User entity

We continue with the User entity defined earlier.

import jakarta.persistence.*;@Entity
public class User {
  @Id @GeneratedValue
  private Long id;
  private String name;
  // getters/setters omitted
}

Code: Service CRUD

The service wraps repository calls and adds business rules if needed.

import org.springframework.stereotype.Service;import java.util.*;
@Service
public class UserService {
  private final UserRepository repo;
  public UserService(UserRepository repo) { this.repo = repo; }

  public List<User> all() { return repo.findAll(); }
  public User add(User u) { return repo.save(u); }
  public Optional<User> find(Long id) { return repo.findById(id); }
  public void delete(Long id) { repo.deleteById(id); }
}

Code: Controller with service

The controller now calls the service instead of repository directly.

import org.springframework.web.bind.annotation.*;import java.util.*;@RestController
@RequestMapping("/users")
public class UserController {
  private final UserService service;
  public UserController(UserService service) { this.service = service; }

  @GetMapping
  public List<User> all() { return service.all(); }

  @PostMapping
  public User add(@RequestBody User u) { return service.add(u); }

  @GetMapping("/{id}")
  public User one(@PathVariable Long id) { return service.find(id).orElse(null); }

  @DeleteMapping("/{id}")
  public void delete(@PathVariable Long id) { service.delete(id); }
}

Benefits

Benefits: cleaner controllers, reusable logic, easier testing, and clearer architecture.

Usage

Test endpoints: POST /users, GET /users, GET /users/{id}, DELETE /users/{id}.

Service layer check

Quick check: Why is a service layer useful in Spring Boot?

Recap

Recap: Built a service layer with CRUD, used it in a controller, and saw benefits of separation.

Frequently asked questions

Is the “CRUD Service Layers” lesson free?

Yes — the full text of “CRUD Service Layers” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “CRUD Service Layers”?

Implement a service layer that provides CRUD operations for entities. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “CRUD Service Layers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. JPA/H2 & Repositories
  2. CRUD Service Layers
  3. Error Handling & Advice
← Back to Java Academy