0–5 min
Real Hook
5–20 min
Core Concept
20–40 min
Live Coding
40–55 min
You Code It
55–60 min
Cheat Sheet
🔥 Real-World Hook (0–5 min)

Every time you open Swiggy, 15 different APIs fire in parallel: auth check, restaurant list, your cart, promotions, location, payment methods.

Each one returns JSON. Each one is secured. Each one handles errors gracefully. Today you build APIs like a senior developer — validation, error handling, pagination, and security.

🔒 Professional REST API Design (5–20 min)

💡 Rules of Good APIs

✔ Use nouns for resources: /students not /getStudents
✔ Use HTTP verbs for actions: GET=read, POST=create, PUT=update, DELETE=remove
✔ Return proper status codes: 200=OK, 201=Created, 404=Not Found, 400=Bad Request
✔ Validate input: never trust the client
✔ Return consistent error format

💻 Live Coding — Production-Grade API (20–40 min)

// DTO — only expose what clients need
public record StudentRequest(
    @NotBlank String name,
    @Email    String email,
    @Min(1) @Max(4) int year
) {}

// Global exception handler
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(Exception ex) {
        return ResponseEntity.status(404)
            .body(new ErrorResponse("NOT_FOUND", ex.getMessage()));
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
        String msg = ex.getBindingResult().getFieldErrors().stream()
            .map(e -> e.getField() + ": " + e.getDefaultMessage())
            .collect(Collectors.joining(", "));
        return ResponseEntity.badRequest().body(new ErrorResponse("VALIDATION_ERROR", msg));
    }
}

// Paginated endpoint
@GetMapping
public Page<Student> getAll(
    @RequestParam(defaultValue = "0") int page,
    @RequestParam(defaultValue = "10") int size
) {
    return repo.findAll(PageRequest.of(page, size));
}
🎯 You Code It (40–55 min)
  1. Add spring-boot-starter-validation to your pom.xml
  2. Create a ProductRequest DTO with: @NotBlank name, @Positive price, @NotBlank category
  3. Add @Valid to your POST endpoint's @RequestBody parameter
  4. Create a GlobalExceptionHandler class that returns JSON error messages
  5. Test: POST with an empty name field — should return 400 with error message
  6. Add pagination to GET all products: ?page=0&size=5

📄 Day 9 Cheat Sheet — REST API

Validation Annotations

@NotBlankNot null & not empty
@NotNullNot null
@EmailValid email format
@Min(n) @Max(n)Number range
@Size(min=2,max=50)String length
@PositiveGreater than 0

HTTP Status Codes

200 OKSuccess
201 CreatedResource created
204 No ContentDeleted / empty
400 Bad RequestInvalid input
404 Not FoundResource missing
500 Server ErrorUnexpected crash

Exception Handling

@RestControllerAdviceGlobal handler
@ExceptionHandler(Ex.class)Handle specific
ResponseEntity.badRequest()400 response
ResponseEntity.status(404)404 response
ex.getMessage()Error text

Pagination

extends JpaRepositoryAlready has it
PageRequest.of(page, size)Create pageable
Page<T> findAll(Pageable p)Get page
page.getTotalElements()Total count
page.getTotalPages()Total pages
?page=0&size=10Query params