CRUD API Development
CRUD for Orders is create, read, update, delete mapped to HTTP.
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
- Implement in-memory
OrderServicewith aConcurrentHashMap. - Wire five controller methods to that service.
- Add not-found behavior via an exception the advice will map (or temporarily ResponseEntity).
- Curl the happy path for create → get → list → update → delete.
Informative example
Delete returns 204.
@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
OrderViewfor 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.
1BeginnerQuestionWhat status code is common for successful DELETE?+
Answer
2IntermediateQuestionWhere should 'cannot cancel shipped order' live?+
Answer
3AdvancedQuestionPUT vs PATCH for Orders?+
Answer
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.