从CXF服务实现添加HTTP标头

时间:2014-09-24 14:01:24

标签: java web-services soap cxf jax-ws

我目前正在实施肥皂服务,我需要从另一个肥皂服务(服务A)拨打一个肥皂服务(服务B)。从服务A,如何设置HTTP标头而不是SOAP服务B的SOAP标头。

目前我正在使用JaxWsDynamicClientFactory,如下所示,

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(localhost/services/test?wsdl");

results = client.invoke(new QName(namespace,operation), service parameters); 

1 个答案:

答案 0 :(得分:3)

您可以使用CXF RequestContext添加标头。例如,要添加名为Header-Name的标头,其值为headerValue

    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient(localhost/services/test?wsdl");
    @SuppressWarnings("unchecked")
    Map<String, List<String>> headers = (Map<String, List<String>>) client.getRequestContext()
            .get(Message.PROTOCOL_HEADERS);

    if (headers == null) {
        headers = new TreeMap<String, List<String>>(
                String.CASE_INSENSITIVE_ORDER);
        client.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
    }       
    headers.put("Header-Name", Collections.singletonList("headerValue"));
    results = client.invoke(new QName(namespace,operation), service parameters);