Axis2创建WebService实例.doc
文本预览下载声明
Axis2创建WebService实例博客分类:
Java综合
WebServiceTomcatApacheWebXML?
??一、Axis2的下载和安装
???? 1.可从/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文件放到Tomcat安装目录\webapps目录中,???? 并启动Tomcat,在浏览器地址栏中输入如下的URL:???? http://localhost:8080/axis2/,如看到axis2的主页面则安装成功。
?
??二、编写和发布WebService
?? (1)用POJO形式发布(无需配置)
????在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布成WebService。?? ?其中POJO中所有的public方法将被发布成WebService方法。????示例代码如下:
????
Java代码 ?
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文件放到Tomcat安装目录\webapps\axis2\WEB-INF\pojo目录中??(如果没有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
??页面显示如下结果:
Xml代码 ?
ns:sayHelloToPersonResponse?xmlns:ns=/axis2??
????returnhello,bill/return???
/ns:sayHelloToPersonResponse??
ns:sayHelloToPersonResponse xmlns:ns=/axis2
returnhello,bill/return
/ns:sayHelloToPersonResponse
?
??在编写、发布和测试WebService时应注意如下几点:???? 1. POJO类不能使用package关键字声明包。
???? 2. Axis2在默认情况下可以热发布WebService,也就是说,将WebService的.class文件复制到pojo目录中时,??????? Tomcat不需要重新启动就可以自动发布WebService。??????? 如果想取消Axis2的热发布功能,可以打开Tomcat安装目录\webapps\axis2\WEB-INF\conf\axis2.xml,??????? 找到如下的配置
显示全部