gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:33:00 26 4
gpt4 key购买 nike

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

到目前为止,我已经实现了过滤器,并且运行良好,使用过滤器的最佳实践是什么?

最佳答案

这里引用 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.

关于java - 设置和获取 HTTP header 的最佳选项是什么?使用 Apache CXF 拦截器或过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com