在Spring-WS中将子元素添加到自定义SOAP标头中

时间:2010-10-27 16:26:10

标签: soap header spring-ws saaj

我使用Spring-WS调用SOAP Web服务。有问题的webservice要求我在SOAP标头中传递一些信息,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <CustomHeaderElement>
         <clientID>xyz</clientID>
         <wsdlVersion>1.0</wsdlVersion>
         <serviceType>ExampleService_v1</serviceType>
      </CustomHeaderElement>
   </soapenv:Header>
   <soapenv:Body>
   ...
   </soapenv:Body>
</soapenv:Envelope>

我已经弄清楚如何使用顶级CustomHeaderElement,但我没有在Spring-WS API中看到任何允许我添加子元素的内容。以下是我到目前为止的情况:

WebServiceTemplate template = ...;

template.marshalSendAndReceive(request, new WebServiceMessageCallback(){
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
        SoapMessage soapMessage = (SoapMessage)message;
        SoapHeader soapHeader = soapMessage.getSoapHeader();
        QName qName = new QName("CustomHeaderElement");
        SOAPHeaderElement headerElement = soapHeader.addHeaderElement(qName);
        //would like to do something like headerElement.addChild(clientIdNode);
    }
});

问题是headerElement似乎没有暴露任何实际添加子项的方法。我知道我可以添加一个属性,但这不是我需要的服务调用。有谁知道如何将必要的子元素添加到我的自定义标题中?

4 个答案:

答案 0 :(得分:8)

我遇到了同样的问题,这是我的解决方案,但它只适用于复杂的简单元素:

template.marshalSendAndReceive(request, new WebServiceMessageCallback(){
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
        SaajSoapMessage soapMessage = (SaajSoapMessage) message;
        SoapHeaderElement messageId =  soapMessage.getSoapHeader().addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "MessageID", "wsa"));
        messageId.setText("urn:abcdef1234");
    }
});

它产生以下XML:

<SOAP-ENV:Header>
  <wsa:MessageID>urn:abcdef1234</wsa:MessageID>
</SOAP-ENV:Header>

BTW javax.xml.soap.SOAPMessage也可以使用,请参见此处:http://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html#bnbia

答案 1 :(得分:3)

final String datPrefix = "dat";
final String datNamespaceUri = "UPRS/Services/IProviderDataManagement/Datatypes";

final String mesPrefix = "mes";
final String mesNamespaceUri = "UPRS/Services/IProviderDataManagement/Messages";

SoapEnvelope se = s.getEnvelope();
se.addNamespaceDeclaration(mesPrefix,
        mesNamespaceUri);
se.addNamespaceDeclaration(datPrefix,
        datNamespaceUri);

SoapMessage s = (SoapMessage) message;

Element root = new Element("requestContext", mesPrefix, mesNamespaceUri);
Element child = new Element("commandId", datPrefix, datNamespaceUri).addContent(guid);
root.addContent(child);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new JDOMSource(root), s.getSoapHeader().getResult());

它产生了输出

<SOAP-ENV:Header>
        <mes:requestContext xmlns:mes="UPRS/Services/IProviderDataManagement/Messages">
            <dat:commandId xmlns:dat="UPRS/Services/IProviderDataManagement/Datatypes">ba7b1e13-8a06-49b6-a264-fc0298f55f4f</dat:commandId>
        </mes:requestContext>
    </SOAP-ENV:Header>

答案 2 :(得分:1)

我对这个解决方案不满意,但事实证明你实际上可以将消息转发给SOAPMessage,它允许你完全访问所有SAAJ api。从那里你可以在标题中构建你想要的任何元素。

答案 3 :(得分:0)

我遇到了同样的问题,并使用以下代码解决了这个问题:

Result result = ((SoapMessage) message).getSoapHeader().getResult();
webServiceTemplate.getMarshaller().marshal(createCustomHeader(), result);

createCustomerHeader()方法创建一个从XSD生成的JAXB bean。