Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Implement GET /products/{id} in JAX-RS so it returns 304 Not Modified when the client's If-None-Match matches the product's current version, and sets Cache-Control for private caching for 60 seconds.

We inject @Context Request, build an EntityTag from the product's version column, and call request.evaluatePreconditions(etag). If the client's If-None-Match matches, it returns a ResponseBuilder already set to 304 Not Modified, which we complete with the same ETag and Cache-Control; otherwise we return 200 with the body, the ETag and a CacheControl that has setPrivate(true) and setMaxAge(60). Using the version column means we never hash the whole entity to produce the tag.

Why this question is asked

Conditional requests are one of the cheapest performance wins in HTTP. When nothing changed, the server sends a few hundred bytes instead of the whole representation, and the client reuses what it already has. Interviewers ask for this implementation because JAX-RS has built-in support that many developers re-implement badly, and because the details, which headers to repeat, weak versus strong tags, where the tag comes from, show real understanding. In this answer, we will recap how ETags and Cache-Control work, write the endpoint, follow two requests through it, and then look at weak tags and the cost of computing the tag.

1A Quick Recap of ETags and Cache-Control

An ETag, entity tag, is a short string the server attaches to a response to identify that version of the resource, for example ETag: "7". The client keeps it with the cached copy. Next time, it sends If-None-Match: "7", meaning send me the body only if your current tag is different. If the tag still matches, the server answers 304 Not Modified with no body, and the client uses its copy.

Cache-Control tells caches how long they may reuse a response without asking at all. private means only the user's own cache, such as the browser, may store it, not a shared proxy or CDN, which is right for data that depends on who is asking. max-age=60 means the copy is fresh for 60 seconds; after that, the client revalidates with If-None-Match. So the two work together: for the first minute the browser does not even ask, and after that it asks cheaply.

2Writing the Endpoint

JAX-RS provides the comparison logic through Request.evaluatePreconditions, which reads If-None-Match, If-Match and the date-based headers for us.

Java
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
public class ProductResource {
@GET
@Path("/{id}")
public Response get(@PathParam("id") long id, @Context Request request) {
long version = products.versionOf(id) // SELECT version FROM product WHERE id = ?
.orElseThrow(NotFoundException::new);
EntityTag etag = new EntityTag(Long.toString(version)); // strong tag: "7"
CacheControl cc = new CacheControl();
cc.setPrivate(true);
cc.setMaxAge(60); // Cache-Control: private, max-age=60
// (some implementations also add no-transform by default; call cc.setNoTransform(false) to drop it)
Response.ResponseBuilder notModified = request.evaluatePreconditions(etag);
if (notModified != null) {
return notModified.tag(etag).cacheControl(cc).build(); // 304, no body
}
ProductDto dto = products.load(id); // only now load the full product
return Response.ok(dto).tag(etag).cacheControl(cc).build(); // 200 with body
}
}

Notice the order of work. We read only the version first, a tiny query, and load and serialise the full product only when the client really needs it. evaluatePreconditions returns null when the request should proceed normally, and a builder preset to the right status when it should not: 304 for a GET or HEAD whose If-None-Match matches, and 412 for other cases such as a failed If-Match.

3Following Two Requests

Here is what the client and server exchange over time.

Java
GET /products/42
-> 200 OK
ETag: "7"
Cache-Control: private, max-age=60
{"id":42,"name":"Kettle","price":39.90}
(30 seconds later: the browser reuses its copy, no request at all)
(90 seconds later)
GET /products/42
If-None-Match: "7"
-> 304 Not Modified
ETag: "7"
Cache-Control: private, max-age=60
(no body)
(after the price changes, version becomes 8)
GET /products/42
If-None-Match: "7"
-> 200 OK
ETag: "8"
Step throughRevalidating with If-None-Match
BrowserProductResourceDatabase
GET /products/42, If-None-Match: "7"
SELECT version WHERE id = 42
7
evaluatePreconditions("7"): match
304, ETag "7", Cache-Control

5 messages in order. Play them, or step through them one at a time.

The full product is never loaded or serialised when nothing changed.

4Weak Versus Strong ETags

A strong ETag promises the bytes are identical. A weak ETag, written W/"7" and created with new EntityTag("7", true), only promises the content is semantically equivalent, for example the same data with different JSON whitespace or a different compression. For If-None-Match, HTTP uses weak comparison, so either kind works for 304 responses. Strong tags are required where byte-for-byte identity matters: range requests, where a client resumes a download from byte 1,000,000 and must know the bytes have not shifted, and If-Match for safe updates in some APIs.

Our version-based tag describes the data, not the exact bytes; if the server sometimes gzips the response and sometimes does not, the bytes differ while the tag stays the same. Strictly, that makes it a weak tag, and many APIs mark it W/ for honesty. For JSON APIs without range requests, either choice works in practice, but we should be consistent.

5Why the Version Column Is the Right Source

A common alternative is hashing the serialised body, for example an MD5 of the JSON. It produces a correct strong tag, but it means loading the entity, mapping it and serialising it on every request, just to discover that we did not need to send it. That throws away most of the benefit. A version column, often the same one JPA uses for @Version optimistic locking, changes on every update and costs one indexed lookup. An updated_at timestamp works too, as long as two updates in the same millisecond cannot share it.

There is one trap. If the representation includes data from other tables, such as the product's category name, a change to the category does not bump the product's version, and clients keep a stale name. In that case the tag must combine the versions of everything in the representation, for example "7-3" for product version 7 and category version 3.

6Follow-up questions

Which headers should a 304 response repeat?

HTTP says a 304 must include the headers that a 200 would have sent among Cache-Control, Content-Location, Date, ETag, Expires and Vary. The reason is that caches update their stored copy's metadata from the 304, so if we omit Cache-Control, the cache may not know the copy is fresh for another 60 seconds. That is why our code adds tag(etag) and cacheControl(cc) to the 304 builder too.

What about Last-Modified and If-Modified-Since?

They are the date-based version of the same idea, and evaluatePreconditions has an overload that takes a Date, or both a Date and an EntityTag. Dates have one-second resolution, so two changes in the same second can be missed; ETags from a version are more precise, and when both are present, If-None-Match takes precedence.

Does private, max-age=60 mean the data can be 60 seconds stale?

Yes, for that user's browser. If that is not acceptable, we use no-cache, which despite its name allows storing but requires revalidation every time, so every request becomes a cheap conditional request.

7Conclusion

In this answer, we implemented conditional GET in JAX-RS. We built an EntityTag from the product's version column, let request.evaluatePreconditions compare it with If-None-Match, returned its 304 builder with the same ETag and Cache-Control when it matched, and otherwise returned 200 with the body, the tag and a CacheControl set to private with max-age 60. We saw that a 304 should repeat headers such as ETag and Cache-Control, that weak tags are fine for semantically equal content while strong ones are needed for byte ranges, and that a version column avoids loading and hashing the entity.

What to say in the interviewAbout 80 seconds aloud

Open with

I would inject the JAX-RS Request with @Context and start by reading only the product's version from the database, which is a cheap indexed lookup. From that I build an EntityTag, so version 7 becomes ETag quote 7 quote.

Then, if they want more

I also build a CacheControl with setPrivate true and setMaxAge 60, because the data is per user and may be reused for a minute without asking. Then I call request.evaluatePreconditions with the tag.

If the client's If-None-Match matches, it returns a ResponseBuilder already set to 304, and I add the same tag and cache control and return it with no body. A 304 should repeat headers like ETag, Cache-Control and Vary so caches can refresh their metadata.

If it returns null, I load the full product and return Response.ok with the DTO, the tag and the cache control. The point of using the version column is that we never load and serialise the entity just to find out the client already has it, which a body hash would require.

On weak versus strong: a weak tag, W/, says the content is semantically the same, which is enough for If-None-Match; strong tags matter for byte-range requests. And if the representation includes data from other tables, the tag has to include their versions too.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Implement GET /products/{id} in JAX-RS so it returns 304 Not Modified when the client's...": https://scrivio-interview-library.netlify.app/java/jax-rs/a04ccf79bc/