如何使用CXF,JAX-RS和HTTP缓存 [英] How to use CXF, JAX-RS and HTTP Caching
问题描述
CXF JAXRS通过处理If-Match,If-Modified-Since和ETags标头提供对许多高级HTTP功能的支持。 JAXRS Request上下文对象可用于检查前提条件。 Vary,CacheControl,Cookies和Set-Cookies也受支持。
CXF JAXRS provides support for a number of advanced HTTP features by handling If-Match, If-Modified-Since and ETags headers. JAXRS Request context object can be used to check the preconditions. Vary, CacheControl, Cookies and Set-Cookies are also supported.
我真的很想使用(或至少探索)这些特征。然而,虽然提供支持的声音非常有趣,但它在实现这些功能方面并不是特别有用。有关如何使用If-Modified-Since,CacheControl或ETags的任何帮助或指示?
I'm really interested in using (or at least exploring) these features. However, while "provides support" sounds really interesting, it isn't particularly helpful in implementing such features. Any help or pointers on how to use If-Modified-Since, CacheControl or ETags?
推荐答案
实际上,答案不是特定于CXF - 它是纯JAX-RS:
Actually, the answer isn't specific to CXF - it's pure JAX-RS:
// IPersonService.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
@GET
@Path("/person/{id}")
Response getPerson(@PathParam("id") String id, @Context Request request);
// PersonServiceImpl.java
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
public Response getPerson(String name, 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();
cc.setMaxAge(600);
ResponseBuilder builder = request.evaluatePreconditions(person.getUpdated(), eTag);
if (builder == null) {
builder = Response.ok(person);
}
return builder.cacheControl(cc).lastModified(person.getUpdated()).build();
}
这篇关于如何使用CXF,JAX-RS和HTTP缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!