AXIS通过wsdl2java程序调用Gsoap发布的服务
Gsoap 生成的服务见 以前写的文章。
--》add.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="add"
targetNamespace="http://localhost/add.wsdl"
xmlns:tns="http://localhost/add.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:add"
xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
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/">
<types>
<schema targetNamespace="urn:add"
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:add"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/"/>
<!-- operation request element -->
<element name="add">
<complexType>
<sequence>
<element name="num1" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__add::num1 -->
<element name="num2" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__add::num2 -->
</sequence>
</complexType>
</element>
<!-- operation response element -->
<element name="addResponse">
<complexType>
<sequence>
<element name="sum" type="xsd:int" minOccurs="0" maxOccurs="1" nillable="true"/><!-- ns__add::sum -->
</sequence>
</complexType>
</element>
</schema>
</types>
<message name="addRequest">
<part name="parameters" element="ns:add"/><!-- ns__add::ns__add -->
</message>
<message name="addResponse">
<part name="parameters" element="ns:addResponse"/>
</message>
<portType name="addPortType">
<operation name="add">
<documentation>Service definition of function ns__add</documentation>
<input message="tns:addRequest"/>
<output message="tns:addResponse"/>
</operation>
</portType>
<binding name="add" type="tns:addPortType">
<SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="add">
<SOAP:operation soapAction=""/>
<input>
<SOAP:body parts="parameters" use="literal"/>
</input>
<output>
<SOAP:body parts="parameters" use="literal"/>
</output>
</operation>
</binding>
<service name="add">
<documentation>gSOAP 2.8.3 generated service definition</documentation>
<port name="add" binding="tns:add">
<SOAP:address location="http://localhost/add.cgi"/>
</port>
</service>
</definitions>
--》新建批处理 通过 wsdl2java 生成代码
set Axis_Lib=E:/mysoft/axis-1_4/lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=E:/mysoft/axis-1_4/mytest
set Package=com.sf
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% add.wsdl
--》测试代码
/**
* 方式一 通过查看 wsdl 直接访问
*/
package com.sf;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:5555/?wsdl";
// 直接引用远程的wsdl文件
// 以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("urn:add", "add"));// WSDL里面描述的接口名称
call.addParameter("num1",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter("num2",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
Object[] c = { 6, 2 };
String result = (String) call.invoke(c);
// 给方法传递参数,并且调用方法
System.out.println("result is " + result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
/**
* 方式二 通过wsdl2java
*/
package com.sf;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class Test2 {
public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {
Add_Service service = new Add_ServiceLocator();
AddPortType client = service.getadd(new java.net.URL("http://localhost:5555/?wsdl")) ;
// AddPortType client = service.getadd() ;
int result = client.add(5,6);
System.out.println(result);
}
}
--》javaScript 测试
var call=new WS.Call(wsAddress);
var nsuri='urn:add';
var type_int=new WS.QName('int','http://www.w3.org/2000/10/XMLSchema');
var type_string=new WS.QName('string','http://www.w3.org/2000/10/XMLSchema');
var qn_op1=new WS.QName('add',nsuri);
//var qn_op1_resp=new WS.QName('getAttrTypeResponse',nsuri);
//top.add=add;
function add(num1,num2) {
call.invoke_rpc(
qn_op1
,new Array(
{name:'num1',value:num1,xsitype:type_int}
,{name:'num2',value:num2,xsitype:type_int}
)
,null
,function (call,envelope) {
var xbody=envelope.get_body().get_all_children()[0].get_all_children()[0].get_value();
// top.createMenuByString(xbody,x,y);
console.log(xbody);
}
)
}
--》下一步进行非基本类型数据交互
--》参考
Java调用以WSDL形式发布的web service .
http://blog.csdn.net/boy_wh520/article/details/1601756
使用AXIS调用WSDL描述的Web服务
http://www.blogjava.net/mrcold/archive/2009/06/17/220044.html
wsdl -axis-webservise(测试通过) .
http://blog.csdn.net/renhui15688/article/details/4261026
Gsoap 生成的服务见 以前写的文章。
--》add.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="add"
targetNamespace="http://localhost/add.wsdl"
xmlns:tns="http://localhost/add.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:add"
xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
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/">
<types>
<schema targetNamespace="urn:add"
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:add"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/"/>
<!-- operation request element -->
<element name="add">
<complexType>
<sequence>
<element name="num1" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__add::num1 -->
<element name="num2" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__add::num2 -->
</sequence>
</complexType>
</element>
<!-- operation response element -->
<element name="addResponse">
<complexType>
<sequence>
<element name="sum" type="xsd:int" minOccurs="0" maxOccurs="1" nillable="true"/><!-- ns__add::sum -->
</sequence>
</complexType>
</element>
</schema>
</types>
<message name="addRequest">
<part name="parameters" element="ns:add"/><!-- ns__add::ns__add -->
</message>
<message name="addResponse">
<part name="parameters" element="ns:addResponse"/>
</message>
<portType name="addPortType">
<operation name="add">
<documentation>Service definition of function ns__add</documentation>
<input message="tns:addRequest"/>
<output message="tns:addResponse"/>
</operation>
</portType>
<binding name="add" type="tns:addPortType">
<SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="add">
<SOAP:operation soapAction=""/>
<input>
<SOAP:body parts="parameters" use="literal"/>
</input>
<output>
<SOAP:body parts="parameters" use="literal"/>
</output>
</operation>
</binding>
<service name="add">
<documentation>gSOAP 2.8.3 generated service definition</documentation>
<port name="add" binding="tns:add">
<SOAP:address location="http://localhost/add.cgi"/>
</port>
</service>
</definitions>
--》新建批处理 通过 wsdl2java 生成代码
set Axis_Lib=E:/mysoft/axis-1_4/lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=E:/mysoft/axis-1_4/mytest
set Package=com.sf
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% add.wsdl
--》测试代码
/**
* 方式一 通过查看 wsdl 直接访问
*/
package com.sf;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:5555/?wsdl";
// 直接引用远程的wsdl文件
// 以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("urn:add", "add"));// WSDL里面描述的接口名称
call.addParameter("num1",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter("num2",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
Object[] c = { 6, 2 };
String result = (String) call.invoke(c);
// 给方法传递参数,并且调用方法
System.out.println("result is " + result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
/**
* 方式二 通过wsdl2java
*/
package com.sf;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class Test2 {
public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {
Add_Service service = new Add_ServiceLocator();
AddPortType client = service.getadd(new java.net.URL("http://localhost:5555/?wsdl")) ;
// AddPortType client = service.getadd() ;
int result = client.add(5,6);
System.out.println(result);
}
}
--》javaScript 测试
var call=new WS.Call(wsAddress);
var nsuri='urn:add';
var type_int=new WS.QName('int','http://www.w3.org/2000/10/XMLSchema');
var type_string=new WS.QName('string','http://www.w3.org/2000/10/XMLSchema');
var qn_op1=new WS.QName('add',nsuri);
//var qn_op1_resp=new WS.QName('getAttrTypeResponse',nsuri);
//top.add=add;
function add(num1,num2) {
call.invoke_rpc(
qn_op1
,new Array(
{name:'num1',value:num1,xsitype:type_int}
,{name:'num2',value:num2,xsitype:type_int}
)
,null
,function (call,envelope) {
var xbody=envelope.get_body().get_all_children()[0].get_all_children()[0].get_value();
// top.createMenuByString(xbody,x,y);
console.log(xbody);
}
)
}
--》下一步进行非基本类型数据交互
--》参考
Java调用以WSDL形式发布的web service .
http://blog.csdn.net/boy_wh520/article/details/1601756
使用AXIS调用WSDL描述的Web服务
http://www.blogjava.net/mrcold/archive/2009/06/17/220044.html
wsdl -axis-webservise(测试通过) .
http://blog.csdn.net/renhui15688/article/details/4261026