Spring-WS:如何提取Soap Message Body

时间:2016-07-13 04:56:17

标签: spring spring-ws

我正试图在spring-ws应用程序中从soap请求中提取soap body。我的肥皂要求是

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org  /soap/envelope/" xmlns:sch="http://www.manager.cts.com/schema">
 <soapenv:Header/>
   <soapenv:Body>
     <sch:addManagerRequest>
         <sch:name>shivani</sch:name>
         <sch:salary>1231231</sch:salary>
         <sch:developer>
            <sch:firstName>asd</sch:firstName>
            <sch:lastName>asdasd</sch:lastName>
            <sch:salary>123123</sch:salary>
         </sch:developer>
     </sch:addManagerRequest>
  </soapenv:Body>
</soapenv:Envelope>

我尝试使用以下代码提取肥皂体:

@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
    SoapMessage message = (SoapMessage) messageContext.getRequest();
    SoapBody soapBody = message.getSoapBody();

    Source bodySource = soapBody.getSource();
    DOMSource bodyDomSource = (DOMSource) bodySource;

    Node bodyNode = bodyDomSource.getNode();
    System.out.println(bodyNode.getNodeValue());
    System.out.println(bodyNode.getChildNodes());
 }

输出是:

 null
 [soapenv:Body: null]

请帮我解决这个问题。我是spring-ws的新手

1 个答案:

答案 0 :(得分:3)

最好的解决方案是使用Jaxb unmarshal()方法。您的java映射文件文件应该有适当的注释: - 示例:

 @XmlRootElement(name = "name_of_request")
 public class Request {

   @XmlElement(required = true) // add this annotation for each field value
   protected String test; 
   //other fields + getter and setter

正确映射文件后,请使用jaxb为您解组对象: -

    SoapMessage message = (SoapMessage) messageContext.getRequest();
    SoapBody soapBody = message.getSoapBody();
    Source bodySource = soapBody.getPayloadSource();
    DOMSource bodyDomSource = (DOMSource) bodySource;

    JAXBContext context = JAXBContext.newInstance(Request.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    Request request = (Request) unmarshaller.unmarshal(bodyDomSource);
    // populate request object.