使用Axis2-1.6.1实现第一个Web Service
教程是对51CTO视频教程的文字总结及个人实践
首先下载Axis2,我下载Binary Distribution的axis2-1.6.1-bin.zip和WAR Distribution的axis2-1.6.1-war.zip。
安装
将axis2-1.6.1-war.zip解压得到的axis2.war文件复制到Tomcat安装目录下webapps文件夹中,启动Tomcat即可。Tomcat安装目录下webapps文件夹中生成了axis2文件夹。
编写Web Service类
编写一个SimpleServicce类:
public class SimpleService{
public String getGreeting(String name){
return "hello " + name;
}
public int getPrice(){
return new java.util.Random().nextInt(100);
}
}
打开cmd命令窗口,使用javac SimpleService.java命令编译得到SimpleService.class文件。
发布Web Service
将Web Service类编译生成的.class文件,即上一步得到的SimpleService.class文件复制到Tomcat安装目录\webapps\axis2\C:\Tomcat\webapps\axis2\WEB-INF\pojo文件夹中,没有pojo文件夹请自行创建。不用重启Tomcat该SimpleService已经热发布成功。可以使用如下URL查看已经发布的Web Service列表:
http://localhost:8080/axis2/services/listServices
点击SimpleService可以看到关于该Web Service的wsdl描述我的本地地址为http://localhost:8080/axis2/services/SimpleService?wsdl
其中<xs:element name="getGreeting">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
定义了getGreeting方法参数name为args0,接下来可以使用URL测试getGreeting方法:http://localhost:8080/axis2/services/SimpleService/getGreeting?args0=world
得到如下结果:
<ns:getGreetingResponse xmlns:ns="http://ws.apache.org/axis2">
<return>hello world</return>
</ns:getGreetingResponse>
下一篇将介绍使用java RPC方法调用Web Service和wsdl2java命令的使用。