Status, headers and body - as opposed to returning the body and accepting 200.
@PostMapping("/orders")
ResponseEntity<Order> create(@RequestBody OrderRequest req) {
Order saved = service.create(req);
return ResponseEntity
.created(URI.create("/orders/" + saved.id()))
.header("X-Request-Id", MDC.get("requestId"))
.body(saved);
}
HTTP/1.1 201 Created
Location: /orders/42
X-Request-Id: 8f2c...
{"id":42,...}
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-08-11