简体   繁体   English

使用客户端时,禁用Apache CXF上的FastInfoset(强制XML)

[英]Disable FastInfoset (force XML) on Apache CXF when using the client

I need to force Apache CXF to send XML requests and handle only XML responses when I'm client and I have no control over the server or the configuration (I saw this but it only apply to the server). 当我是客户端而我无法控制服务器或配置时,我需要强制Apache CXF发送XML请求并仅处理XML响应(我看到了这一点,但它只适用于服务器)。 Right now it's always using FastInfoset which is great for performances but is causing some issue and I would like to be able to disable it but I didn't find much information on FastInfoset on the Internet and how should go about disabling it. 现在它总是使用FastInfoset,这对于性能很好,但是引起了一些问题,我希望能够禁用它,但我没有在Internet上找到关于FastInfoset的更多信息,应该如何禁用它。 Do you have any clues? 你有线索吗?

Thank you in advance for any help. 预先感谢您的任何帮助。

This is only possible if the remote server supports pure XML responses. 仅当远程服务器支持纯XML响应时,才可以执行此操作。 Most of the servers which do support both Fastinfoset and pure XML are looking into Accept header from the request to decide in which format to return the response. 大多数支持Fastinfoset和纯XML的服务器都在查看请求中的Accept标头,以决定返回响应的格式。 So you can try to force the XML response by sending the Accept: application/xml header with each of your requests. 因此,您可以尝试通过向每个请求发送Accept: application/xml标头来强制执行XML响应。 For that you will need to create a CXF out interceptor and register it with your application. 为此,您需要创建一个CXF out拦截器并将其注册到您的应用程序中。

Following Interceptor will always set Accept: application/xml Interceptor之后将始终设置Accept: application/xml

public class HttpHeaderInterceptor extends AbstractPhaseInterceptor<Message>{
    public XmlOnlyInterceptor() {
        super(Phase.POST_PROTOCOL);
    }
    @Override
    public void handleMessage(Message message) throws Fault {
        Map<String, List> headers = (Map<String, List>)message.get(Message.PROTOCOL_HEADERS);
        headers.put("Accept", Collections.singletonList("application/xml"));
    }
}

To register it use follwoing configuration 要注册它,请使用以下配置

<jaxws:client id="clientBean" serviceClass="org.example.service.ServicePortType"
          address="example.org/src/service/ServicePort">
    <jaxws:outInterceptors>
        <bean class="org.example.interceptor.HttpHeaderInterceptor"/>
    </jaxws:outInterceptors>
</jaxws:client>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM