java / beginner
Snippet
Flexible Responses with ResponseEntity
ResponseEntity represents the entire HTTP response. You can use it to configure the status code, headers, and the body, providing more control than returning a raw object.
snippet.java
1
2
3
4
5
6
7
@GetMapping("/check")public ResponseEntity<String> checkStatus() {return ResponseEntity.status(HttpStatus.OK).header("X-Version", "1.0").body("Service is active");}
spring
Breakdown
1
ResponseEntity<String>
A wrapper for the response containing a String body.
2
.status(HttpStatus.OK)
Explicitly sets the HTTP status to 200 OK.
3
.header("X-Version", "1.0")
Adds a custom HTTP header to the response.