TLDR; How can I send a text message in the body along with a 304 status code using Spring ResponseEntity
?
Context
I am writing a REST API with Spring-boot. In some endpoints, I want to return:
- either status
200 OK
with bodySuccess
, - or status
304 NOT MODIFIED
with bodyNot modified.
My endpoints use ResponseEntity
(in kotlin) in the following way:
@PutMapping("/test")
fun modifyStuff(): ResponseEntity<String> {
if (someCondition)
// "not modified" not sent in the body
return ResponseEntity("not modified", HttpStatus.NOT_MODIFIED)
// using OK, it works
return ResponseEntity("success", HttpStatus.OK)
}
Problem
Whenever I create a ResponseEntity
with a status code != 200, the body is not sent (empty body). Changing the HttpStatus
to OK
makes the message show again... I don't want to create error handlers for not modified, as this is definitely not an error.