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

    ResponseEntity

    ResponseEntity<T> gives you full control over the HTTP response: status, headers, body.

    Course progress0%
    Focus
    2 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    ResponseEntity<T> gives you full control over the HTTP response: status, headers, body. Use it whenever 200 OK is not exactly right.

    Informative example

    ts
    @PostMapping
    public ResponseEntity<OrderDto> create(@Valid @RequestBody NewOrder cmd) {
    OrderDto created = svc.create(cmd);
    URI location = ServletUriComponentsBuilder.fromCurrentRequest()
    .path("/{id}").buildAndExpand(created.id()).toUri();
    return ResponseEntity.created(location)
    .header("X-Trace-Id", MDC.get("traceId"))
    .body(created);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
    svc.delete(id);
    return ResponseEntity.noContent().build(); // 204
    }
    Ready to mark this lesson complete?Track your journey across the entire course.