我正在考虑从使用JAX RS的Apache CXF RS切换到Spring MVC REST,并看到Spring MVC REST当前处理ETag的方式存在一些问题.也许我不理解正确,或者有更好的方法来实现JAX RS目前正在做的事情?
使用Apache CXF RS,在REST服务内部评估上次修改时间戳和ETag的条件(条件评估实际上非常复杂,请参阅RFC 2616第14.24和14.26节,所以我很高兴这对我来说是完成的).代码看起来像这样:
@GET
@Path("...")
@Produces(MediaType.APPLICATION_JSON)
public Response findBy...(..., @Context Request request) {
... result = ...fetch-result-or-parts-of-it...;
final EntityTag eTag = new EntityTag(computeETagValue(result), true);
ResponseBuilder builder = request.evaluatePreconditions(lastModified, eTag);
if (builder == null) {
// a new response is required, because the ETag or time stamp do not match
// ...potentially fetch full result object now, then:
builder = Response.ok(result);
} else {
// a new response is not needed, …Run Code Online (Sandbox Code Playgroud)