0
votes

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 body Success,
  • or status 304 NOT MODIFIED with body Not 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.

1
Per Http specification 304 response should not have a body. - Andriy Budzinskyy

1 Answers

0
votes

Earlier there was earlier standard RFC2616 and now you can refer to newer RFC 7230-7237 and both of them mention 304 response should not include body.

Specifically older RFC2616 says it "must not include body" and newer RFC7230 "All 1xx (Informational), 204 (No Content), and 304 (Not Modified) responses do not include a message body" In the end some servers might send or accept body with this status but it is not the case for spring ResponseEntity.