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

    ResponseEntity

    ResponseEntity lets Orders set status code, headers, and body together.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    ResponseEntity<T> lets Orders set status code, headers, and body together. Use it when @ResponseStatus is too coarse — for example create with Location, or conditional GETs later.

    Returning a bare OrderView is still fine for simple 200s. Reach for ResponseEntity when the success status varies or headers matter.

    Prefer the fluent builders: ResponseEntity.ok(), .created(uri), .noContent(), .notFound(). Avoid magic integer statuses.

    Empty bodies pair with 204. Do not return ResponseEntity<Void> with a body by accident.

    Purpose of this lesson

    Choose when Orders handlers return ResponseEntity versus a raw DTO, and build 201/204 responses correctly.

    Understanding the topic

    Status + headers + body are one response object.

    • ok(body): 200 with JSON.
    • created(location).body(body): 201 + Location.
    • noContent(): 204.
    • notFound().build(): 404 without a body (advice can still centralize errors).
    • Consistency: pick one style per endpoint family.

    Step-by-step explanation

    1. Convert create to ResponseEntity.created.
    2. Convert delete to ResponseEntity.noContent().
    3. Leave simple get as a bare record if you prefer — or wrap with ok for uniformity.
    4. Curl -i and read status lines and headers.

    Informative example

    Create with Location.

    java
    @PostMapping
    ResponseEntity<OrderView> create(@Valid @RequestBody CreateOrderRequest body) {
    OrderView created = orders.place(body);
    URI location = URI.create("/api/orders/" + created.id());
    return ResponseEntity.created(location).body(created);
    }
    @DeleteMapping("/{id}")
    ResponseEntity<Void> delete(@PathVariable long id) {
    orders.delete(id);
    return ResponseEntity.noContent().build();
    }

    Real-world use

    API gateways and mobile clients cache based on status and Location. Returning 200 for create forces every client to special-case body inspection. TechLearningPRO Orders standardizes on 201+Location for new checkouts.

    Security implications

    Headers can leak internals.

    • Do not put sensitive tokens in custom response headers.
    • Be careful with cache headers on authenticated order responses.
    • 404 vs 403: avoid confirming resource existence to unauthorized users when that is sensitive.

    Best practices

    • Builders over manual constructors.
    • Centralize error responses in advice; use ResponseEntity mainly for success variations.
    • Test status codes in MockMvc/WebTestClient.

    Common mistakes

    • Using new ResponseEntity<>(body, HttpStatus.OK) everywhere when builders are clearer.
    • Returning 200 with null body instead of 404.
    • Setting Location to a relative path your clients cannot resolve — document the expected form.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhen do you need ResponseEntity instead of a raw DTO?+

    Answer

    When status or headers vary — create/Location, no-content deletes, or negotiated responses.
    2BeginnerQuestionHow do you return 201 with a body?+

    Answer

    ResponseEntity.created(uri).body(dto).
    3IntermediateQuestionShould errors also use ResponseEntity in every controller?+

    Answer

    Prefer throwing domain exceptions handled by @ControllerAdvice so error shape stays consistent.

    Hands-on exercise

    Lab. Implement create/delete with ResponseEntity. Assert 201+Location and 204 in an integration test or with curl -i.

    Summary

    ResponseEntity gives Orders precise HTTP control. Next you formalize request/response DTOs.

    Ready to mark this lesson complete?Track your journey across the entire course.