I want to add etag attribute in each response. I've added the vary-header and a cache-control header (with max-age=600, public) to the responses but i did not find any solution to add etag in response. Can anyone help me please?
Asked
Active
Viewed 740 times
1 Answers
0
The ETag header is just an additional header like the cache-control header you already added. Have a look at the following sample code for generating a ETag header in a JAX-RS resource:
@GET
@Path("/yourResource/{id}")
public Response getPerson(@PathParam("id") String name, @Context Request request){
CacheControl cc = new CacheControl();
cc.setMaxAge(86400);
Response.ResponseBuilder rb = null;
EntityTag etag = new EntityTag(someService.getById(id).hashCode()+"");
responseBuilder = req.evaluatePreconditions(etag);
if (responseBuilder != null) {
return responseBuilder.cacheControl(cc).tag(etag).build();
}
responseBuilder = Response.ok(UserDatabase.getUserById(id)).cacheControl(cc).tag(etag);
return responseBuilder .build();
}

rieckpil
- 10,470
- 3
- 32
- 56
-
1It means i have to implement a method to set etag for each response. Is there any way to configure it in standalone.xml? – Mohit Chaudhary Sep 18 '18 at 07:16
-
yes you either have to add this code in your JAX-RS resource or define a general interceptor for all off your requests, if you need this. – rieckpil Sep 18 '18 at 12:45
-
Thanks for reply, Actually i am using struts2 and if i implement a general interceptor then it is not working for static pages(js, css and html files) – Mohit Chaudhary Sep 18 '18 at 13:20
-
you want an etag for all of your files? Not only JAX-RS resources? – rieckpil Sep 18 '18 at 14:22
-
Yes, for all java script files and css files. – Mohit Chaudhary Sep 18 '18 at 14:25