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

    REST API Basics

    REST for Orders means resources, verbs, and status codes that match client expectations.

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

    Introduction

    REST for Orders means resources, verbs, and status codes that match client expectations. An order is a resource at /api/orders/{id}. Creating returns 201 with the body (and ideally a Location header). Missing resources return 404, not an empty 200.

    Idempotency matters: GET, PUT, and DELETE should be safe to retry; POST create needs an idempotency key or a natural unique constraint. PATCH applies partial updates deliberately.

    Boot does not make your API RESTful by itself. Mapping annotations are tools; resource design is yours.

    Stay consistent: plural nouns, JSON bodies, and predictable error shapes (next lessons cover ResponseEntity and @ControllerAdvice).

    Purpose of this lesson

    Apply REST resource rules to Orders endpoints: correct verbs, status codes, and idempotent reads/updates.

    Understanding the topic

    Clients program to status codes as much as to JSON fields.

    • GET: read; no side effects.
    • POST: create or non-idempotent actions; return 201 when a resource is born.
    • PUT: replace a known id; idempotent.
    • PATCH: partial update when you support it explicitly.
    • DELETE: remove; often 204 with empty body.

    Step-by-step explanation

    1. List Orders operations: get, create, cancel.
    2. Assign verb + path + success status for each.
    3. Decide create idempotency strategy before mobile ships retries.
    4. Reject RPC-style paths like /api/createOrder in review.

    Informative example

    Create returns 201.

    java
    @PostMapping
    ResponseEntity<OrderView> create(@Valid @RequestBody CreateOrderRequest body) {
    OrderView created = orders.place(body);
    URI location = ServletUriComponentsBuilder
    .fromCurrentRequest()
    .path("/{id}")
    .buildAndExpand(created.id())
    .toUri();
    return ResponseEntity.created(location).body(created);
    }

    Real-world use

    Partner integrations retry POSTs. Without idempotency, TechLearningPRO double-charges seats. REST verbs alone are not enough — but choosing POST for create and 201 for success is the shared language ops and client teams expect.

    Security implications

    REST structure does not equal secure access.

    • Authorize every id-bearing route.
    • Do not leak whether an order exists to unauthorized callers if that is sensitive.
    • Prefer POST bodies over sensitive data in query strings (logged by proxies).

    Best practices

    • Plural resource names: /api/orders.
    • Return 404 for unknown ids via exception handling — not null JSON.
    • Document idempotency for every POST the mobile app may retry.

    Common mistakes

    • Returning 200 for create and forcing clients to guess whether an id is new.
    • Using GET for cancel/charge actions.
    • Encoding actions as verbs in paths (/doCreate).

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhich status code for a successful create?+

    Answer

    201 Created, often with a Location header and representation body.
    2IntermediateQuestionWhy is GET supposed to be safe?+

    Answer

    Clients and intermediaries assume GET can be retried and cached. Side effects on GET cause duplicate charges and cache corruption.
    3AdvancedQuestionHow do you make POST create safer under retries?+

    Answer

    Idempotency keys, unique business constraints, and deduplication in the service — not only REST naming.

    Hands-on exercise

    Lab. Implement create with ResponseEntity.created. Curl -i and confirm 201 plus a Location header pointing at the new id.

    Summary

    Orders speaks REST with clear resources and codes. Next you implement full CRUD on that model.

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