客户端的请求报文格式如下,并且格式不能改动
<?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);
}
再次测试验证,结果通过。