Create: POST, 201, Location
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
implement creation the standard way.
Planned topics
POST /categorieswith a JSON body creates a new item. The server picks the ID.- Bind the body with
@RequestBodyinto a request DTO (a plain record). - Return
201 Createdand aLocationheader with the URL of the new item. - Return the created representation in the body so the client sees the ID.
- Invalid input gives
400; validation is covered in JNR-P2 and applied here.
Draft outline details
Demonstration
@PostMapping
ResponseEntity<CategoryResponse> create(@RequestBody CreateCategoryRequest req) {
CategoryResponse c = service.create(req);
URI location = URI.create("/categories/" + c.id());
return ResponseEntity.created(location).body(c);
}Check your understanding
Which header holds the new item's URL?
Show answer
Location.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.