Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 17

    CRUD API Development

    CRUD for Orders is create, read, update, delete mapped to HTTP.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    CRUD for Orders is create, read, update, delete mapped to HTTP. In Fundamentals the service can still be in-memory; the HTTP and DTO shapes should match what JPA will persist later.

    Typical set: POST /api/orders, GET /api/orders/{id}, GET /api/orders, PUT /api/orders/{id} (or PATCH), DELETE /api/orders/{id}. Each method is a one-liner into OrderService.

    Updates must define semantics: replace whole representation vs partial. Deletes must define 404 vs 204 when the id is already gone.

    Do not invent five overlapping endpoints for the same state change. One clear cancel operation beats three aliases.

    Purpose of this lesson

    Expose a coherent Orders CRUD surface with service delegation and consistent success/error status behavior.

    Understanding the topic

    CRUD is a checklist, not an excuse for anemic design.

    • Create: 201 + body (+ Location).
    • Read one / many: 200 + body; unknown id → 404.
    • Update: 200 with new state, or 204 if you return no body.
    • Delete: 204 common; decide idempotent delete behavior.
    • Service: owns invariants (cannot cancel shipped orders, etc.).

    Step-by-step explanation

    1. Implement in-memory OrderService with a ConcurrentHashMap.
    2. Wire five controller methods to that service.
    3. Add not-found behavior via an exception the advice will map (or temporarily ResponseEntity).
    4. Curl the happy path for create → get → list → update → delete.

    Informative example

    Delete returns 204.

    java
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    void delete(@PathVariable long id) {
    orders.delete(id); // throws OrderNotFoundException if missing
    }

    Real-world use

    Support tools need list+get; checkout needs create; ops needs cancel. If cancel is modeled as DELETE vs POST /cancel, pick one and stick to it across TechLearningPRO clients.

    Security implications

    CRUD multiplies attack surface by five.

    • Authorize list differently from get-by-id if lists are admin-only.
    • Prevent mass assignment: do not bind entity fields clients must not set.
    • Rate-limit create if it triggers paid side effects.

    Best practices

    • Keep CRUD controllers boring; put rules in the service.
    • Use the same OrderView for create/get/update responses when possible.
    • Add integration tests for status codes, not only JSON fields.

    Common mistakes

    • Update endpoints that silently create missing ids (upsert) without documenting it.
    • Delete that returns 200 with a body nobody uses.
    • Duplicating validation in every CRUD method instead of shared request records.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat status code is common for successful DELETE?+

    Answer

    204 No Content when there is no body to return.
    2IntermediateQuestionWhere should 'cannot cancel shipped order' live?+

    Answer

    In the service (domain rule), with the controller translating the domain exception to an HTTP error.
    3AdvancedQuestionPUT vs PATCH for Orders?+

    Answer

    PUT replaces the representation you support; PATCH applies a partial update schema. Pick one and document fields that are immutable (id, createdAt).

    Hands-on exercise

    Lab. Implement in-memory CRUD for Orders. Prove create→get→delete→get returns 404 on the last get once exception handling is in place.

    Summary

    Orders CRUD is the HTTP skeleton persistence will fill. Next you control how Jackson shapes the JSON.

    Ready to mark this lesson complete?Track your journey across the entire course.