#Java访问GSoap发布的WebService接口
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033
目录
环境:
编译器:MyEclipse 2014
系统环境:Windows 7 64bit
相关阅读:GSoap发布WebService
##1.下载axis
从官网下载axis相关jar包,地址:http://axis.apache.org/axis/
##2.核心代码
核心代码:
package webserviceClinet;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class webserviceClient
{
public static void main(String[] args)
{
try
{
//直接运行java Application
String endpoint = "http://127.0.0.1:8088/myStr.wsdl";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(endpoint);
// WSDL里面描述的接口名称(要调用的方法)
call.setOperationName(new QName("urn:myStr", "myStrCat"));
// 接口方法的参数名, 参数类型,参数模式 IN(输入), OUT(输出) or INOUT(输入输出)
call.addParameter("str1In", XMLType.XSD_STRING, ParameterMode.IN);//该参数必须与wsdl中一致,否则可能传输过去为空值
call.addParameter("str2In", XMLType.XSD_STRING, ParameterMode.IN);//该参数必须与wsdl中一致,否则可能传输过去为空值
call.addParameter("paramOut", XMLType.XSD_STRING, ParameterMode.OUT);//该参数必须与wsdl中一致
// 设置被调用方法的返回值类型
call.setReturnType(XMLType.XSD_STRING);
//设置方法中参数的值
String str = "{\"userID\":\"123456\",\"userName\":\"杰克\"}";//userID
//String strParam = new String(str.getBytes(Charset.forName("UTF-8")));
Object[] paramValues = new Object[] {"123","456"};
// 给方法传递参数,并且调用方法
String result = (String)call.invoke(paramValues);
//打印结果
System.out.println("C++ result: " + result);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
WSDL 文件如下:
<definitions xmlns:tns="http://localhost:8088/myStr.wsdl" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="urn:myStr" xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:HTTP="http://schemas.xmlsoap.org/wsdl/http/" xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/" xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" name="myStr" targetNamespace="http://localhost:8088/myStr.wsdl">
<types>
<schema xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="urn:myStr" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:myStr" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
</schema>
</types>
<message name="myStrCatRequest">
<part name="str1In" type="xsd:string"/>
<!-- ns__myStrCat::str1In -->
<part name="str2In" type="xsd:string"/>
<!-- ns__myStrCat::str2In -->
</message>
<message name="myStrCatResponse">
<part name="paramOut" element="ns:paramOut"/>
<!-- ns__myStrCat::paramOut -->
</message>
<portType name="myStrPortType">
<operation name="myStrCat">
<documentation>Service definition of function ns__myStrCat</documentation>
<input message="tns:myStrCatRequest"/>
<output message="tns:myStrCatResponse"/>
</operation>
</portType>
<binding name="myStr" type="tns:myStrPortType">
<SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="myStrCat">
<SOAP:operation style="rpc" soapAction=""/>
<input>
<SOAP:body parts="Body" use="literal" namespace="urn:myStr"/>
</input>
<output>
<SOAP:body parts="Body" use="literal" namespace="urn:myStr"/>
</output>
</operation>
</binding>
<service name="myStr">
<documentation>gSOAP 2.8.31 generated service definition</documentation>
<port name="myStr" binding="tns:myStr">
<SOAP:address location="http://localhost:8088"/>
</port>
</service>
</definitions>
##3.运行结果
##4.注意事项
###4.1入口参数
入口参数必须与wsdl一致,否则会出现传输空值的问题
觉得文章对你有帮助,可以扫描二维码捐赠给博主,谢谢!
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033