Spring's support for If-Match Header

1.9k views Asked by At

Coming to HTTP conditional request, briefly, benefiting from the Etag and If-*, we can realize response cache(Etag+If-None-Match) and optimistic lock(Etag+If-Match).

As I see, it's convenient to perform the response cache using Spring which provides the specific filer ShallowEtagHeaderFilter to generate Etag value and check it against If-None-Match header. However, I cannot find corresponding components in Spring to do optimistic lock. Therefore, how can I implement that in Spring?

1

There are 1 answers

0
arcuri82 On

I had the same issue. Didn't find any native way in Spring to check it. But you can always extract the headers and do manual comparisons. For example (in Kotlin):

@PutMapping
fun update(         
        @RequestHeader("If-Match") ifMatch: String?
) : ResponseEntity<Void>{

    if(ifMatch != null && ifMatch.trim() != computeETag()){
        return ResponseEntity.status(412).build()
    }