I have the following endpoint:
@ApiResponses(value = {
@ApiResponse(responseCode = "204", content = {@Content(schema = @Schema(implementation = UserDTO.class))}, description = "Successful")
})
@DeleteMapping("/Users/{id}")
public ResponseEntity<String> deleteUser(@PathVariable("id") String id) {
userService.deleteUser(id);
return ResponseEntity.noContent().header("Successfully deleted", "0").build();
}
And I need to customise 204 response - return not just status and empty body but 204 code + body which will be contained "Successfully deleted" message. I've tried to add header manually (in code above), but it didn't work properly. Could you give me a piece of advice - how can I add it by different way?
As Marc correctly pointed out in his comment, returning 204
and a body at the same time makes no sense, as that is not what 204
signifies. I suggest you simply return the following:
return new ResponseEntity<>("Successfully deleted", HttpStatus.OK);
You don't really need any custom headers for what you described, but if you still feel the need for one, you can do it like so
HttpHeaders headers = new HttpHeaders();
headers.add("custom-header", "value");
return new ResponseEntity<>("Successfully deleted", headers, HttpStatus.OK);