设置和获取 HTTP headers 的最佳选项是什么??使用 Apache CXF 拦截器或过滤器

What is the best option to set and get HTTP headers?? Using Apache CXF interceptor or a Filter

在我的一个项目中,我必须在响应中设置 ETag header 并阅读 if-none-matched header 在传入请求中。截至目前,我已经使用 Apache CXF Filters 实现了此功能,但是正如我搜索和发现的那样,同样的功能也可以通过使用 interceptors 来完成。如果我继续使用 CXF 过滤器,我将不得不经历哪些显着优点 and/or 缺点?

截至目前,我已经实现了过滤器并且工作正常使用过滤器的最佳实践是什么?

引用自http://cxf.apache.org/docs/jax-rs-filters.html

Difference between JAXRS filters and CXF interceptors JAXRS runtime flow is mainly implemented by a pair of 'classical' CXF interceptors. JAXRSInInterceptor is currently at Phase.UNMARSHAL (was at Phase.PRE_STREAM before CXF 2.2.2) phase while JAXRSOutInterceptor is currently at Phase.MARSHAL phase.

JAXRS filters can be thought of as additional handlers. JAXRSInInterceptor deals with a chain of Pre and Post Match ContainerRequestFilters, just before the invocation. JAXRSOutInterceptor deals with a chain of ContainerResponseFilters, just after the invocation but before message body writers get their chance.

Sometimes you may want to use CXF interceptors rather than writing JAXRS filters. For example, suppose you combine JAXWS and JAXRS and you need to log only inbound or outbound messages. You can reuse the existing CXF interceptors :

<beans>
<bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

<jaxrs:server> 
  <jaxrs:inInterceptors>
    <ref bean="logInbound"/>
  </jaxrs:inInterceptors>
  <jaxrs:outInterceptors>
    <ref bean="logOutbound"/>
  </jaxrs:outInterceptors>
</jaxrs:server>
<jaxws:endpoint>
 <jaxws:inInterceptors>
 <ref bean="logInbound"/>
 </jaxws:inInterceptors>
 <jaxws:outInterceptors>
<ref bean="logOutbound"/>
 </jaxws:outInterceptors>
</jaxws:endpoint>
</beans>

Reusing other CXF interceptors/features such as GZIP handlers can be useful too.

At the moment it is not possible to override a response status code from a CXF interceptor running before JAXRSOutInterceptor, like CustomOutInterceptor above, which will be fixed.