Skip to content
Ameen Abozaid
Planned lessonJNR-019-L01

Create: POST, 201, Location

  • Lesson 1 of 8
  • Junior
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 /categories with a JSON body creates a new item. The server picks the ID.
  • Bind the body with @RequestBody into a request DTO (a plain record).
  • Return 201 Created and a Location header 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

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.