如何使用Java Web Service在同一个信封中处理多个SOAP请求?

时间:2014-07-23 15:41:22

标签: java web-services soap

我目前有一个使用以下方法的网络服务:

@Override
@WebResult(Name="OIPResponse")
public  Map<String, Object> getOIP(@WebParam(name = "invoice") String invoiceNumber,@WebParam(name = "part") String partNumber)

我用来调用它的普通SOAP请求如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oip="http://oip.mycompany.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <oip:getOIP>
         <invoice>41587182</invoice>
         <part>9ZF2A5-570</part>
      </oip:getOIP>
   </soapenv:Body>
</soapenv:Envelope>

并且响应是这样的:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:getOIPResponse xmlns:ns2="http://oip.mycompany.com/">
            <OIPResponse>
                <entry>
                    <key>ERR_CODE</key>
                </entry>
                <entry>
                    <key>SELLING_OU</key>
                    <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">null</value>
                </entry>
                ...
            </OIPResponse>
        </ns2:getOIPResponse>
    </soap:Body>
</soap:Envelope>

我已经对此进行了测试,但效果很好。现在我想知道是否有办法在同一个SOAP信封中包含多个请求,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oip="http://oip.mycompany.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <oip:getOIP>
         <invoice>41587182</invoice>
         <part>9ZF2A5-570</part>
      </oip:getOIP>
      <oip:getOIP>
         <invoice>41587183</invoice>
         <part>9ZF2A5-570</part>
      </oip:getOIP>
      <oip:getOIP>
         <invoice>41587184</invoice>
         <part>9ZF2A5-570</part>
      </oip:getOIP>
      <oip:getOIP>
         <invoice>41587185</invoice>
         <part>9ZF2A5-570</part>
      </oip:getOIP>
   </soapenv:Body>
</soapenv:Envelope>

并取回这样的东西:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:getOIPResponse xmlns:ns2="http://oip.mycompany.com/">
            <OIPResponse>
               ...
            </OIPResponse>
            <OIPResponse>
               ...
            </OIPResponse>
            <OIPResponse>
               ...
            </OIPResponse>
               ...
        </ns2:getOIPResponse>
    </soap:Body>
</soap:Envelope>

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

我最终创建了一个名为OIPRequest的POJO,其中包含我需要的两个参数(发票和部件号)以及一个新的方法getOIPMultiple,它将一个OIPRequests数组作为输入参数。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class OIPRequest {
    public String invoiceNumber, partNumber;

    public OIPRequest(String invoice, String part) {
        invoiceNumber = invoice;
        partNumber = part;
    }

    public OIPRequest() {
        invoiceNumber = "";
        partNumber = "";
    }
} 

这使请求看起来像这样:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <getMultipleOIP xmlns="http://oip.mycompany.com/">
            <OIPRequest xmlns="">
                <invoiceNumber>41587182</invoiceNumber>
                <partNumber>9ZF2A5-570</partNumber>
            </OIPRequest>
            <OIPRequest xmlns="">
                <invoiceNumber>41587182</invoiceNumber>
                <partNumber>9ZF2A5-570</partNumber>
            </OIPRequest>
            <OIPRequest xmlns="">
                <invoiceNumber>41587182</invoiceNumber>
                <partNumber>9ZF2A5-570</partNumber>
            </OIPRequest>
        </getMultipleOIP>
    </Body>
</Envelope>