??一、Axis2的下载和安装
???? 1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本:
????? 可以下载如下两个zip包:
????? axis2-1.5.4-bin.zip
????? axis2-1.5.4-war.zip
????? 其中 axis2-1.5.4-bin.zip文件中包含了Axis2中所有的jar文件,
????? axis2-1.5.4-war.zip文件用于将WebService发布到Web容器中。
???? 2.将axis2-1.5.4-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到webapps目录中,
???? 并启动Tomcat,在浏览器地址栏中输入如下的URL:
????http://localhost:8080/axis2/,如看到axis2的主页面则安装成功。
?
??二、编写和发布WebService
??(1)用POJO形式发布(无需配置)
????在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布成WebService。
?? ?其中POJO中所有的public方法将被发布成WebService方法。
????示例代码如下:
????
- public?class?HelloService?{? ??
- ????public?String?sayHello(){ ??
- ????????return?"hello"; ??
- ????}??? ??
- ????public?String?sayHelloToPerson(String?name){???????? ??
- ????????if(name==null){ ??
- ????????????name?=?"nobody"; ??
- ????????} ??
- ????????return?"hello,"+name; ??
- ????} ??
- }??
public class HelloService {
public String sayHello(){
return "hello";
}
public String sayHelloToPerson(String name){
if(name==null){
name = "nobody";
}
return "hello,"+name;
}
}
???编译HelloService类后,将HelloService.class文件放到webappsaxis2WEB-INFpojo目录中
??(如果没有pojo目录,则建立该目录)。现在我们已经成功将HelloService类发布成了WebService。
??在浏览器地址栏中输入如下的URL:
????http://localhost:8080/axis2/services/listServices
??在浏览器地址栏中输入如下的两个URL来分别测试sayHelloToPerson和sayHello方法:
??? 1.http://localhost:8080/axis2/services/HelloService/sayHello
??? 2.http://localhost:8080/axis2/services/HelloService/sayHelloToPerson?name=bill
??页面显示如下结果:
- <</SPAN>ns:sayHelloToPersonResponse?xmlns:ns="http://ws.apache.org/axis2">??
- ????<</SPAN>return>hello,bill</</SPAN>return>? ??
- </</SPAN>ns:sayHelloToPersonResponse>??
hello,bill
?
??在编写、发布和测试WebService时应注意如下几点:
???? 1. POJO类不能使用package关键字声明包。
???? 2. Axis2在默认情况下可以热发布WebService,也就是说,将WebService的.class文件复制到pojo目录中时,
??????? Tomcat不需要重新启动就可以自动发布WebService。
??????? 如果想取消Axis2的热发布功能,可以打开webappsaxis2WEB-INFconfaxis2.xml,
??????? 找到如下的配置代码:
- <</SPAN>parameter?name="hotdeployment">true</</SPAN>parameter>??
true
?
? 将true改为false即可。要注意的是,Axis2在默认情况下虽然是热发布,但并不是热更新.
??也就是说,一旦成功发布了WebService,再想更新该WebService,就必须重启Tomcat。
??这对于开发人员调试WebService非常不方便,因此,在开发WebService时,可以将Axis2设为热更新。
??在axis2.xml文件中找到
- <</SPAN>parameter?name="hotupdate">false</</SPAN>parameter>??
false
?
??? 将false改为true即可。
???? 3. 在浏览器中测试WebService时,如果WebService方法有参数,需要使用URL的请求参数来指定该WebService方法
???? 参数的值,请求参数名与方法参数名要一致,例如,要测试sayHelloToPerson方法,请求参数名应为name,如上面的URL所示。
???? 4. 发布WebService的pojo目录只是默认的,如果读者想在其他的目录发布WebService,
???? 可以打开axis2.xml文件,并在元素中添加如下的子元素:
- <</SPAN>deployer?extension=".class"?directory="my"?class="org.apache.axis2.deployment.POJODeployer"/>??
?
??上面的配置允许在webappsaxis2WEB-INFmy目录中发布WebService。
? ?例如,将本例中的HelloService.class复制到my目录中也可以成功发布
?? (但要删除pojo目录中的SimpleService.class,否则WebService会重名)。
?
(2)使用services.xml配置文件发布
??用Axis2实现Web Service,虽然可以将POJO类放在axis2WEB-INFpojo目录中直接发布成Web Service,
??这样做不需要进行任何配置,但这些POJO类不能在任何包中。这似乎有些不方便.
??为此,Axis2也允许将带包的POJO类发布成Web Service。先实现一个POJO类,代码如下:
- package?com.sinosoft.webservice; ??
-
public?class?HelloServiceNew?{?????????? ??
- ????public?String?sayHelloNew(){ ??
- ????????return?"hello"; ??
- ????}??????????? ??
- ????public?String?sayHelloToPersonNew(String?name){????? ??
- ????????if(name==null){ ??
- ????????????name?=?"nobody"; ??
- ????????}??????????? ??
- ????????return?"hello,"+name; ??
- ????} ??
- ????public?void?updateData(String?data){ ??
- ????????System.out.println(data+"?已更新。"); ??
- ????} ??
- }??
package com.sinosoft.webservice;
public class HelloServiceNew {
public String sayHelloNew(){
return "hello";
}
public String sayHelloToPersonNew(String name){
if(name==null){
name = "nobody";
}
return "hello,"+name;
}
public void updateData(String data){
System.out.println(data+" 已更新。");
}
}
?
?? 要想将HelloServiceNew类发布成Web Service,需要一个services.xml文件,
?? 这个文件需要放在META-INF目录中,该文件的内容如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。