H HTTP Status Code in Springboot API Endpoint.
In order to specify HTTP Status Code in Spring API Endpoint’s response, there seem to exist multiple options and workarounds.
Listed 2 workarounds are the ones that I found to be straightforward to implement.
- Return
ResponseEntity
- Throw
ResponseStatusException
Return ResponseEntity
.
The ResponseEntity
from org.springframework.http
was an amazing find. With ResponseEntity
, we can add or modify multiple metadata of the response we wish to send.
Example
import org.springframework.http.ResponseEntity;
@DeleteMapping
public ResponseEntity<?> deleteBlock(@RequestBody Block block) {
Boolean blockDeleted = blockService.deleteBlock(block);
Map<String, Object> response = new HashMap<>();
response.put("error", !blockDeleted);
response.put("message", blockDeleted ?
"Successfully deleted the block -> {}".formatted(block) :
"Failed to delete the block.");
return ResponseEntity.status(blockDeleted ?
HttpStatus.OK :
HttpStatus.NOT_FOUND)
.body(response);
}
Throw ResponseStatusException
.
The ResponseStatusException
from org.springframework.web.server
is another simple alternative depending on the use-case but for me, simply to modify the HTTP Status Code, I found ResponseStatusException
to be equally simple.
Example
import org.springframework.web.server.ResponseStatusException;
@PostMapping
public Map<String, Object> registerBlock(@RequestBody Block block) {
Boolean blockRegistered = blockService.addblock(block);
Map<String, Object> response = new HashMap<>();
response.put("error", !blockRegistered);
response.put("message", blockRegistered ?
"Successfully saved the block information." :
"Failed to save the new block in the database.");
if (!blockRegistered) {
throw new ResponseStatusException(
HttpStatus.CONFLICT,
"Block with the email {} already exists.".formatted(
block.getEmail()));
}
return response;
}