oligofren oligofren - 8 months ago 42
Java Question

Is it possible to set ETags using JAX-RS without resorting to Response objects?

In one of the few questions (with answers) I have found on SO regarding JAX-RS and caching, the answer to generating ETags (for caching) is by setting some values on the Response object. As in the following:

public Response getPerson(@PathParam("id") String name, @Context Request request){
Person person = _dao.getPerson(name);

if (person == null) {
return Response.noContent().build();

EntityTag eTag = new EntityTag(person.getUUID() + "-" + person.getVersion());

CacheControl cc = new CacheControl();

ResponseBuilder builder = request.evaluatePreconditions(person.getUpdated(), eTag);

if (builder == null) {
builder = Response.ok(person);

return builder.cacheControl(cc).lastModified(person.getUpdated()).build();

The problem is that will not work for us, since we use the same methods for both SOAP and REST services, by annotating the methods with @WebMethod (SOAP), @GET (and whatever else we might need to expose the service). The previous service would look like this to us (excluding the creation of headers):

public Person getPerson(@WebParam(name="id") @PathParam("id") String name){
return _dao.getPerson(name);

Is there any way - through some extra configuration - of setting those headers? This is the first time I have found that using Response objects actually has some benefit over just auto-conversion ...

We are using Apache CXF.


Yes you might be able to use interceptors to achieve this if you could generate the E-tag AFTER you create your response object.

public class MyInterceptor extends AbstractPhaseInterceptor<Message> {

    public MyInterceptor () {

    public final void handleMessage(Message message) {
        MultivaluedMap<String, Object> headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS);

        if (headers == null) {
            headers = new MetadataMap<String, Object>();

        //generate E-tag here
        String etag = getEtag();
        String cc = 600;

        headers.add("E-Tag", etag);
        headers.add("Cache-Control", cc);
        message.put(Message.PROTOCOL_HEADERS, headers);

If that way isn't viable, I would use the original solution that you posted, and just add your Person entity to the builder:

Person p = _dao.getPerson(name);
return builder.entity(p).cacheControl(cc).lastModified(person.getUpdated()).build();