对webservices蒙了一天,研究了使用axis来开发webservices的过程,做记录。
我使用的工具:myeclipse7 , axis1.4, tomcat6
1、新建一个web工程,把axis的jar复制到web工程的lib文件夹下面。
2、新建一个java类,还是最简单。
package server;
public class SayHello {
public String sayName() {
return "hello";
}
}
3、在web-info下面新建文件server-config.wsdd
填充配置信息
<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler" xmlns="http://xml.apache.org/axis/wsdd/">
<globalConfiguration name="defaultClientConfig">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session"/>
</handler>
<handler name="Handler2" type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request"/>
<parameter name="extension" value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<transport name="http" type="">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="URLMapper"/>
<handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
</transport>
<transport name="local" type="">
<responseFlow name="ResponseFlow1" type="">
<handler name="Handler1" type="LocalResponder"/>
</responseFlow>
</transport>
<!--这里配置了一个Web Service,如果有多个Web Service,就按这种格式在下面增加即可-->
<service name="SayHello" provider="java:RPC">
<parameter name="allowedMethods" value="*" />
<parameter name="className" value="server.SayHello" />
</service>
</deployment>
4、此时打开http://localhost:8080/test/services,会出现
And now… Some Services
SayHello (wsdl)
sayName
SayHello (wsdl) 是可以点击的。
5、使用axis的工具WSDL2Java来制作客户端,在dos窗口下进入web-info文件夹,输入命令:
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/test/services/SayHello?wsdl
就会得到四个客户端的java文件了!
6、测试文件
package localhost.test.services.SayHello;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class TestSayHello {
public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {
SayHelloServiceLocator locator = new SayHelloServiceLocator();
URL wddlUrl = new URL("http://localhost:8080/test/services/SayHello?wsdl");
SayHello say = locator.getSayHello(wddlUrl);
System.out.print(say.sayName());
}
}
这样就能在控制台看到hello了!
做的是一个非常简单的例子,步骤也没有过多的介绍,只为做个记录!
提供一个完整的下载地址:
webservicesTest.rar