天天看點

jax-ws中SOAPBinding的使用姿勢

用戶端的請求封包格式如下,并且格式不能改動

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ns1:MessageHeader xmlns:ns1="http://XX.com/AA/checkData" soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0">
            <ns1:AppID>AA</ns1:AppID>
            <ns1:Provide_AppID>BB</ns1:Provide_AppID>
        </ns1:MessageHeader>
    </soapenv:Header>
    <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ns2:checkData xmlns:ns2="http://XX.XX.com:8080/PendDataImport_app/services/PendDataImport" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <ns2:lastTime xsi:type="xsd:string">2018-01-20 00:23:14</ns2:lastTime>
            <ns2:item xsi:type="xsd:string">aaaa</ns2:item>
        </ns2:checkData>
    </soapenv:Body>
</soapenv:Envelope>
           

用戶端在每個請求方法裡面添加了自定義的命名空間,要求服務端跟它保持一緻。

我當時想,這還不簡單,釋出服務的時候,指定命名空間不就好了。代碼如下:

@WebService(targetNamespace = "http://XX.XX.com:8080/PendDataImport_app/services/PendDataImport")
public interface HelloService {
    @WebMethod
    String checkData( @WebParam(name="lastTime")String lastTime, @WebParam(name="item")String item);
}
           

但是測試時,請求封包卻怎麼也通不過。報錯資訊:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Client</faultcode>
         <faultstring>Unmarshalling Error: 意外的元素 (uri:"http://XX.XX.com:8080/PendDataImport_app/services/PendDataImport", local:"lastTime")。所需元素為<{}lastTime>,<{}item></faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>
           

在看https://www.cnblogs.com/fsjohnhuang/articles/2340335.html這篇文章後,有所啟發。

将服務端代碼修改了一點,如下:

@WebService(targetNamespace = "http://XX.XX.com:8080/PendDataImport_app/services/PendDataImport")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloService {
    @WebMethod
    String checkData( @WebParam(name="lastTime")String lastTime, @WebParam(name="item")String item);
}
           

再次測試驗證,結果通過。