Performance: ResponseEntity for full response control
This affects server response time and network payload size, impacting how fast the client receives and renders the response.
public ResponseEntity<String> getData() {
return ResponseEntity
.status(HttpStatus.OK)
.header("Cache-Control", "max-age=3600")
.body("data");
}public String getData() {
return "data";
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Returning raw body string | N/A | N/A | N/A | [OK] |
| Using ResponseEntity with headers and status | N/A | N/A | N/A | [OK] Good |
ResponseEntity in a Spring Boot REST controller?ResponseEntity with status 404 and a body message "Not Found"?status() first, then body().status(404).body("Not Found") correctly. return ResponseEntity.body("Not Found").status(404); reverses the order, which is invalid. return new ResponseEntity<>("Not Found", 404); uses a constructor but 404 should be HttpStatus, not int. return ResponseEntity.create(404, "Not Found"); uses a non-existent method.return ResponseEntity.status(HttpStatus.CREATED)
.header("Location", "/items/123")
.body("Item created");@GetMapping("/test")
public ResponseEntity<String> test() {
return ResponseEntity.body("Hello").status(HttpStatus.OK);
}status() before body(). Here, body() is called first, which is invalid.status() first, then header(), then body().Pass the test and this pattern moves unknown