Search code examples
javaspringspring-boothttp-status-codes

When and Why Should I Use the HTTP 304 Not Modified Status Code in a Spring Application?


I'm currently working on a Spring application and I've come across the HTTP 304 Not Modified status code. While I understand its basic purpose, I'm struggling to grasp real-life scenarios where it would be particularly beneficial.

Could someone please provide some concrete examples or use cases where using the 304 status code would be a good practice?

Additionally, I'd appreciate any insights on how this status code can enhance performance or efficiency in a web application.

Thank you for shedding light on this topic!


Solution

  • Follow the specification.

    10.3.5 304 Not Modified

    If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD
    respond with this status code. The 304 response MUST NOT contain a
    message-body, and thus is always terminated by the first empty line
    after the header fields.

    You might, for example, do a 'conditional GET' on a large document; there's no point in getting it again if you already have it and it hasn't been changed since. The efficiency of not transferring the document should be obvious; you need to trade that off against the work of maintaining a local copy.