REST API Basics
REST for Orders means resources, verbs, and status codes that match client expectations.
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
- List Orders operations: get, create, cancel.
- Assign verb + path + success status for each.
- Decide create idempotency strategy before mobile ships retries.
- Reject RPC-style paths like
/api/createOrderin review.
Informative example
Create returns 201.
@PostMappingResponseEntity<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.
1BeginnerQuestionWhich status code for a successful create?+
Answer
Location header and representation body.2IntermediateQuestionWhy is GET supposed to be safe?+
Answer
3AdvancedQuestionHow do you make POST create safer under retries?+
Answer
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.