如何通过jaxb向SOAP请求添加自定义标头而不使用SoapHandler

时间:2012-09-14 15:46:55

标签: java web-services header jax-ws

我似乎无法在不使用SOAPHandler的情况下找到从Web服务客户端向Soap请求添加自定义标头的方法。我四处寻找替代品,BindingProvider似乎为我做了这项工作。但它没有用。我不知道我在这里错过了什么。 这就是我希望我的请求看起来像:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://xx.xx.xx/xxx/provisioning/ws">
   <soapenv:Header>   
                <ws:XXXSecurity xmlns:ws="http://xx.xx.xx/xx/security/ws">
                <ws:Identification type="TrustedApplicationIdentification">
                <ws:ApplicationId>xx</ws:ApplicationId>
                </ws:Identification>
                </ws:xxSecurity>
      <ws:xxLocale xmlns:ws="http://xx.xx.xx/xx/presentation/webservice/locale">
        <ws:clientLocale>en_US</ws:clientLocale>
      </ws:xxLocale>
   </soapenv:Header>
   <soapenv:Body>
      <ws:GetAllNonProvisionedServers/>
   </soapenv:Body>
</soapenv:Envelope>

这里是代码:

Provisioning service = new Provisioning(url, qName);
ProvisioningPort servicePort = service.getProvisioningPort();

servicePort = service.getProvisioningPort();


Map<QName, List<String>> headersMap = new HashMap<QName, List<String>>();

String mySoapHeader = "<ws:Identification type=\"TrustedApplicationIdentification\">"
    + "<ws:ApplicationId>Password123$</ws:ApplicationId>"
    + "</ws:Identification>" ;//+ "</soapenv:Header>";

List<String> mySOAPHeaders = new ArrayList<String>();
mySOAPHeaders.add(mySoapHeader);
headersMap.put(qSecurityName, mySOAPHeaders);

System.out.println(MessageContext.HTTP_REQUEST_HEADERS);

BindingProvider bp = (BindingProvider) servicePort;
List<Server> findAllNonProvisionedServers = servicePort
            .findAllNonProvisionedServers();

3 个答案:

答案 0 :(得分:0)

我使用了org.apache.axiom.soap.SOAPFactory类来设置我的WS-Addressing SOAP Headers。让我知道您用来调用Web服务的API,以便我可以尝试帮助您。

答案 1 :(得分:0)

        MemberSubmissionEndpointReference endPointReference = new MemberSubmissionEndpointReference();
        Elements referenceParameters = endPointReference.referenceParameters;
        List<Element> elements = referenceParameters.elements;

        SOAPPartImpl soappartImpl = new SOAPPart1_1Impl();
        soappartImpl.addMimeHeader("SOAPAction", "http://www.abc.com/WebServices/UploadService/ProcessUpload");
        SOAPDocumentImpl soapdocumentimpl = new SOAPDocumentImpl(soappartImpl);     
        SOAPHeader ele = new Header1_1Impl(soapdocumentimpl, "");       
        elements.add(ele);

        ProvisioningPort port = service.getPort(endPointReference, EDMOnlineUploadServiceSoap.class, null);

上面的示例将向您展示如何将MIME标头添加到Web服务请求中。 以上代码可以作为参考。但它不是一个完整的解决方案。 在我看来,MemberSubmissionEndpointReference将适用于WS-Addressing。请检查其他标头参数。

还有其他生成ws客户端的方法,可以让您轻松设置标头。请尝试一下。

答案 2 :(得分:0)

谢谢你们,但似乎SOAPHandler是正确的方式,所以我就是这样。