如何使用 CXF、JAX-RS 和 HTTP 缓存
CXF JAXRS 通过处理 If-Match、If-Modified-Since 和 ETags 标头来支持许多高级 HTTP 功能。 JAXRS 请求上下文对象可用于检查先决条件。还支持 Vary、CacheControl、Cookies 和 Set-Cookies。
我真的对使用(或至少探索)这些功能很感兴趣。然而,虽然“提供支持”听起来确实很有趣,但它对于实现这些功能并不是特别有帮助。有关如何使用 If-Modified-Since、CacheControl 或 ETags 的任何帮助或指示吗?
The CXF documentation mentions caching as Advanced HTTP:
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,答案并不特定于 CXF - 它是纯粹的 JAX-RS:
Actually, the answer isn't specific to CXF - it's pure JAX-RS:
随着即将推出的 JAX-RS 2.0,将可以以声明方式应用 Cache-Control,如 http://jalg.net/2012/09/declarative-cache-control-with-jax-rs-2-0/
您至少已经可以测试这个与泽西岛。但不确定 CXF 和 RESTEasy。
With the forthcoming JAX-RS 2.0 it will be possible to apply Cache-Control declaratively, as explained in http://jalg.net/2012/09/declarative-cache-control-with-jax-rs-2-0/
You can already test this at least with Jersey. Not sure about CXF and RESTEasy though.
CXF 没有实现动态过滤,如下所述: http://www.jalg.net/2012/09/declarative-cache-control-with-jax-rs-2-0
如果您使用直接返回您自己的对象而不是 CXF 响应,很难添加缓存控制头。
我找到了一种优雅的方法,即使用自定义注释并创建一个读取此注释并添加标头的 CXF 拦截器。
首先,创建一个 CacheControl 注释
然后,将此注释添加到您的 CXF 操作方法(如果您使用接口,则它适用于接口或实现)
然后创建一个 CacheControl 拦截器来处理该注释并将标头添加到您的响应中。
最后配置 CXF 以使用您的拦截器,您可以在这里找到所有需要的信息: http://cxf .apache.org/docs/interceptors.html
希望它会有所帮助。
卢伊克
CXF didn't implements dynamic filtering as explained here : http://www.jalg.net/2012/09/declarative-cache-control-with-jax-rs-2-0
And if you use to return directly your own objects and not CXF Response, it's hard to add a cache control header.
I find an elegant way by using a custom annotation and creating a CXF Interceptor that read this annotation and add the header.
So first, create a CacheControl annotation
Then, add this annotation to your CXF operation method (interface or implementation it works on both if you use an interface)
Then create a CacheControl interceptor that will handle the annotation and add the header to your response.
Finally configure CXF to use your interceptor, you can find all the needed information here : http://cxf.apache.org/docs/interceptors.html
Hope it will help.
Loïc