java / beginner
Snippet
Customizing Responses with ResponseEntity
ResponseEntity allows you to fully customize the HTTP response, including status codes, headers, and the body.
snippet.java
1
2
3
4
5
6
7
@GetMapping("/check")public ResponseEntity<String> checkStatus() {return ResponseEntity.status(HttpStatus.OK).header("X-Custom-Header", "Valid").body("System is operational");}
spring
Breakdown
1
ResponseEntity<String>
Wraps the response body and metadata.
2
.status(HttpStatus.OK)
Sets the HTTP status code (e.g., 200 OK).