at the moment im trying to enable caching for my jersey restful service.
So there occure some questions.
Whats the the value of the entityTag? Can it just be a unique generated random string?
When I make a post request from my client to the server, I get back the response with the entity tag. Question: how to cache this and how do I know which cached entityTag I have to send for the next get request?
On server side I get the sended entityTag. How do I compare this with the ressource? Because i didnt attach the entityTag to the ressource.
Its just about comparing entityTags. So when do I need the last-modified header value?
Sorry, would be nice to get an example for server and client side. I cant find anything for this issue. How to send entityTags in request, how to compare them on server side and what abouts last-modified.
ETags provide a mechanism for client cache to validate if it's cached content is still up-to-date. Regarding your questions:
On the server side, Jersey provides support for evaluating ETags and generating a response. E.g. your resource method can look like this:
@GET
public Response doGet() {
EntityTag et = yourMethodForCalculatingEntityTagForThisResource();
// the following method call will result in Jersey checking the headers of the
// incoming request, comparing them with the entity tag generated for
// the current version of the resource generates "304 Not Modified" response
// if the same. Otherwise returns null.
ResponseBuilder rb = request.evaluatePreconditions(new EntityTag("1"));
if (rb != null) {
// Jersey generated 304 response - return it
return rb.build();
}
// return the current version of the resource with the corresponding tag
return Response.ok(getCurrentVersion(), "text/plain").tag(et).build();
}
Same kind of support is provided for last-modified header and also both etag and last-modified.
This wikipedia article provides a nice overview of ETags: http://en.wikipedia.org/wiki/HTTP_ETag
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With