用jws来创建web services
用jws来创建web services
用jws来创建web services是一个最简单的方法,下面借助一个简单的例子说明一下具体过程
1.编写一个java文件,经javac编译后没有错误,把该文件另存为.jws文件。例如合HelloWorld.jws:
public class HelloWorld{
public String sayHello(String name){
return 'Hello'+name;
}
}
然后把它放到对应的webapps/axis下.
2。启动服务器,axis会自动把它变成一个服务,生成相关的wsdl文档,并把jws用java编译成class文件放到axis\WEB-INF\jwsClasses目录下。打开IE,输入:http://127.0.0.1:7001/axis/HelloWorld.jws,即可发现有一个wsdl和该服务。
3。编写一应用程序客户端来访问该web services.例如:
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 ServiceClient{
public static void main(String[] args) throws Exception{
String endpoint='http://127.0.0.1:7001/axis/HelloWorld.jws';
String name='gaolong1';
Service service=new Service();
Call call=(Call) service.createCall();
call.setUsername('glong1');
call.setPassword('131001');//这是服务所用的安全的认证
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName('sayHello');
call.addParameter('name',XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);//XMLType.XSD_STRING);
String ret=(String)call.invoke(new Object[] {name});
System.out.println(ret);
}
}
运行后,会显示其调用信息。
在使用的过程中,要用到tcpmontor来监听截取soap消息:
用:java org.apache.axis.utils.tcpmon 7003 localhost 7001 (其中前者是截听得代理端口,中间是目标地址,后者是所用的目标端口)
通过tcpmontor来监听tcp:7001得到的soap消息如下:
请求消息:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope 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'>
<soapenv:Body>
<sayHello soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<name xsi:type='xsd:string'>gaolong1</name>
</sayHello>
</soapenv:Body></soapenv:Envelope>
回应消息:
<?xml version='1.0' encoding='utf-8'? >
<soapenv:Envelope 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'>
<soapenv:Body>
<sayHelloResponse soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<sayHelloReturn xsi:type='xsd:string'>Hellogaolong1</sayHelloReturn>
</sayHelloResponse>
</soapenv:Body></soapenv:Envelope>