一、准备工作
1、安装JDK,我使用的是版本是JDK1.6,下载地址:
http://java.sun.com/javase/downloads/index.jsp
下载那个rpm的bin包,即jdk-6u1-linux-i586-rpm.bin。
上传安装文件到服务器上,并COPY到/usr/local目录下,执行安装
#cd /usr/local
# chmod +x jdk-6u1-linux-i586-rpm.bin
#./jdk-6u1-linux-i586-rpm.bin
修改环境变量:
# vi /etc/profile
在最后添加以下三行
export JAVA_HOME=/usr/java/jdk 1.6.0 _01
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
运行:
#. /etc/profile
#java -version
查看版本是否正确,判断是否安装成功
2、下载并安装myecplise:
http://downloads.myeclipseide.com/downloads/products/eworkbench/6.5.1GA/MyEclipse_6.5.1GA_E3.3.2_Installer.exe
3、下载myecplise的两个插件,分别是axis2-eclipse-codegen-wizard-1.2.1和axis2-eclipse-service-archiver-wizard-1.2
axis2-eclipse-service-archiver-wizard-1.2:
http://apache.justdn.org/ws/axis2/tools/1_2/axis2-eclipse-service-archiver-wizard-1.2.zip
axis2-eclipse-codegen-wizard-1.2.1:
http://apache.justdn.org/ws/axis2/tools/1_1_1/axis2-eclipse-codegen-wizard.zip
将插件包解压,把解压下来的文件拷贝到myecplise安装目录下的eclipse目录下面的plugins目录下。重启myecplise。菜单栏,点击 File -> New -> Other,显示如下:
如上图所示说明插件安装成功。
4、下载 apache-tomcat- 6.0.13 ,下载地址:
http://tomcat.apache.org
安装tomact,解压apache-tomact-6.0.13.tar.gz到/usr/local下,解压命令:
tar -zxvf apache-tomact-6.0.13.tar.gz
把/usr/java/lib目录下的tools.jar拷贝到 /usr/local/apache-tomcat-6.0.13/lib目录下。
在/usr/local/apache-tomcat-6.0.13/bin目录下有两个shell文件startup.sh和shutdown.sh,分别是启动和关闭tomcat的批处理SHELL。
如果想让tomact在开机的时候自动启动,则编辑文件 /etc/rc.d/rc.local
在文件最后添加这一句 :/usr/local/apache-tomcat-6.0.13/bin/startup.sh
rel="File-List" href="file:///C:%5CDOCUME%7E1%5C1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> 重新启动机器或者不重启机器直接运行/usr/local/apache-tomcat-6.0.13/bin/startup.sh启动TOMCAT。访问http://192.168.1.235:8080,可以看到TOMCAT的缺省页面出来了。
windows环境下的tomact安装与此类似,这里不作介绍。
5、下载并安装axis2:http://ftp.wayne.edu/apache/ws/axis2/1_1_1/axis2.war
下载后将后缀改为.jar,解压后,把axis2文件夹拷贝到/usr/local/apache-tomact-6.0.13/webapps目录下。
重启tomact,访问http://192.168.1.235:8080/axis2/services/,可以看到axis2自带的version服务。
二、开发一个Add服务,实现一个加法运算的服务。
1、新建一个工程,点击File -> New -> web project,输入project name,点击Finish。
2、右键选择Add工程,新建一个Interface文件,输入包名和文件名。然后点击Finish。
编写Add接口代码,如下所示:
package com.Add;
public interface Add
{
public int add(int i,int j);
}
右键选择Add.java,选择Export,如下所示,点击next
后面选择next,最后finish,在你的workplace中可以找到你的Add.jar文件。
3、选择File -> New -> Other ,如下图:
选择Axis2 Code Generator
选择next
输入class name ,这里是Add.java的完整路径,即com.Add包下的Add接口
然后点击Add jar,选择在上面export出来的Add.jar文件,再点击Test Class Loading。
然后next
选择Finish,这便创建了一个Add.wsdl的文件,在workplace/Add/src/目录下。
右键选择Add工程,选择Refresh刷新,将可以看到Add.wsdl文件。
4、根据wsdl文件生成代码。
双击Add.wsdl文件,显示如下
作如下修改:
1、安装JDK,我使用的是版本是JDK1.6,下载地址:
http://java.sun.com/javase/downloads/index.jsp
下载那个rpm的bin包,即jdk-6u1-linux-i586-rpm.bin。
上传安装文件到服务器上,并COPY到/usr/local目录下,执行安装
#cd /usr/local
# chmod +x jdk-6u1-linux-i586-rpm.bin
#./jdk-6u1-linux-i586-rpm.bin
修改环境变量:
# vi /etc/profile
在最后添加以下三行
export JAVA_HOME=/usr/java/jdk 1.6.0 _01
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
运行:
#. /etc/profile
#java -version
查看版本是否正确,判断是否安装成功
2、下载并安装myecplise:
http://downloads.myeclipseide.com/downloads/products/eworkbench/6.5.1GA/MyEclipse_6.5.1GA_E3.3.2_Installer.exe
3、下载myecplise的两个插件,分别是axis2-eclipse-codegen-wizard-1.2.1和axis2-eclipse-service-archiver-wizard-1.2
axis2-eclipse-service-archiver-wizard-1.2:
http://apache.justdn.org/ws/axis2/tools/1_2/axis2-eclipse-service-archiver-wizard-1.2.zip
axis2-eclipse-codegen-wizard-1.2.1:
http://apache.justdn.org/ws/axis2/tools/1_1_1/axis2-eclipse-codegen-wizard.zip
将插件包解压,把解压下来的文件拷贝到myecplise安装目录下的eclipse目录下面的plugins目录下。重启myecplise。菜单栏,点击 File -> New -> Other,显示如下:
![](http://blogimg.chinaunix.net/blog/upfile2/080926211547.gif)
4、下载 apache-tomcat- 6.0.13 ,下载地址:
http://tomcat.apache.org
安装tomact,解压apache-tomact-6.0.13.tar.gz到/usr/local下,解压命令:
tar -zxvf apache-tomact-6.0.13.tar.gz
把/usr/java/lib目录下的tools.jar拷贝到 /usr/local/apache-tomcat-6.0.13/lib目录下。
在/usr/local/apache-tomcat-6.0.13/bin目录下有两个shell文件startup.sh和shutdown.sh,分别是启动和关闭tomcat的批处理SHELL。
如果想让tomact在开机的时候自动启动,则编辑文件 /etc/rc.d/rc.local
在文件最后添加这一句 :/usr/local/apache-tomcat-6.0.13/bin/startup.sh
rel="File-List" href="file:///C:%5CDOCUME%7E1%5C1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> 重新启动机器或者不重启机器直接运行/usr/local/apache-tomcat-6.0.13/bin/startup.sh启动TOMCAT。访问http://192.168.1.235:8080,可以看到TOMCAT的缺省页面出来了。
windows环境下的tomact安装与此类似,这里不作介绍。
5、下载并安装axis2:http://ftp.wayne.edu/apache/ws/axis2/1_1_1/axis2.war
下载后将后缀改为.jar,解压后,把axis2文件夹拷贝到/usr/local/apache-tomact-6.0.13/webapps目录下。
重启tomact,访问http://192.168.1.235:8080/axis2/services/,可以看到axis2自带的version服务。
二、开发一个Add服务,实现一个加法运算的服务。
1、新建一个工程,点击File -> New -> web project,输入project name,点击Finish。
![](http://blogimg.chinaunix.net/blog/upfile2/080926214600.gif)
2、右键选择Add工程,新建一个Interface文件,输入包名和文件名。然后点击Finish。
![](http://blogimg.chinaunix.net/blog/upfile2/080926215257.gif)
编写Add接口代码,如下所示:
package com.Add;
public interface Add
{
public int add(int i,int j);
}
右键选择Add.java,选择Export,如下所示,点击next
![](http://blogimg.chinaunix.net/blog/upfile2/080926215638.gif)
![](http://blogimg.chinaunix.net/blog/upfile2/080926215813.gif)
后面选择next,最后finish,在你的workplace中可以找到你的Add.jar文件。
3、选择File -> New -> Other ,如下图:
![](http://blogimg.chinaunix.net/blog/upfile2/080926220144.gif)
![](http://blogimg.chinaunix.net/blog/upfile2/080926220415.gif)
![](http://blogimg.chinaunix.net/blog/upfile2/080926220549.gif)
输入class name ,这里是Add.java的完整路径,即com.Add包下的Add接口
然后点击Add jar,选择在上面export出来的Add.jar文件,再点击Test Class Loading。
然后next
![](http://blogimg.chinaunix.net/blog/upfile2/080927222810.gif)
选择Finish,这便创建了一个Add.wsdl的文件,在workplace/Add/src/目录下。
右键选择Add工程,选择Refresh刷新,将可以看到Add.wsdl文件。
4、根据wsdl文件生成代码。
双击Add.wsdl文件,显示如下
![](http://blogimg.chinaunix.net/blog/upfile2/080927223151.gif)
作如下修改:
![](http://blogimg.chinaunix.net/blog/upfile2/080927223353.gif)
点击 Source,显示如下:
修改红框内的内容:param0改为int1,param1改为int2,可以修改为自己认为有意义的单词。
修改后为:
保存。
鼠标点击 File -> New -> Other,显示如下:
选择next
注意,这里与前面有所不同,应该选择Generate Java source code from a WSDL file。
然后选择next
点击Browse,选择wsdl文件的位置,然后点击next,首先生成服务代码,设置如下所示:
选择next
以下几步生成client测试代码,鼠标点击 File -> New -> Other
next
next
next
next
选择Finish
这时左边将显示如下,出现红X,说明有错误。
作如下三个修改:
1、右键点击Add工程,选择Build Path ,再选择Add External Archives。
显示如下:
选择axis2包下的 WEB-INF/lib/目录下的所以 .jar文件。
2、将com.Add包下的Add.java拖动到src.com.Add包下,若出错选continue。然后删除com.Add包。
右键选择src.com.Add,选择Refactor,再选择Rename
修改后如下所示:
3、点击test.com.add包,修改包里的AddTest.java文件。把第一行的package com.add;改为
package test.com.add;
保存。
此时右键选择Add工程,选择Refresh,红X将消失。
5、编写业务代码。
双击com.add包下的文件AddSkeleton.java。
显示如下代码:
package com.add;
/**
* AddSkeleton java skeleton for the axisService
*/
public class AddSkeleton{
/**
* Auto generated method signature
* @param param0
*/
public com.add.xsd.AddResponse add
(
com.add.xsd.Add param0
)
{
//Todo fill this with the necessary business logic
throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#add");
}
}
修改后如下所示:
package com.add;
/**
* AddSkeleton java skeleton for the axisService
*/
public class AddSkeleton{
/**
* Auto generated method signature
* @param param0
*/
public com.add.xsd.AddResponse add
(
com.add.xsd.Add request
)
{
//Todo fill this with the necessary business logic
com.add.xsd.AddResponse response = new com.add.xsd.AddResponse();
int result = request.getInt1() + request.getInt2();
response.set_return(result);
return response;
}
}
保存。
然后编写测试代码:
双击test.com.add包下的AddTest.java文件
其中testadd()方法的代码显示如下:
public void testadd() throws java.lang.Exception{
com.add.AddStub stub =
new com.add.AddStub();//the default implementation should point to the right endpoint
com.add.AddStub.Add param4=
(com.add.AddStub.Add)getTestObject(com.add.AddStub.Add.class);
// todo Fill in the param4 here
assertNotNull(stub.add(
param4));
}
修改为:
public void testadd() throws java.lang.Exception{
java.lang.String url = "http://192.168.1.235:8080/axis2/services/Add";
com.add.AddStub stub =
new com.add.AddStub(url);//the default implementation should point to the right endpoint
com.add.AddStub.Add request=
(com.add.AddStub.Add)getTestObject(com.add.AddStub.Add.class);
// todo Fill in the param4 here
//在request中设置了两个数字,200和300,则返回结果应该是500
request.setInt1(200);
request.setInt2(300);
System.out.println(stub.add(request).get_return()); //结果为500
assertNotNull(stub.add(
request));
}
6、调试程序。
点击 File -> New -> Other
选择Axis2 Service Archiver,如上图所示,这一步是生成.aar的部署文件,选择next
点击Browse,选择class文件的位置,在Add工程目录下的WebRoot目录下的WEB-INF下的Class文件中。
next
选择WSDL,文件的位置,如上图所示。
直接点next,如上图所示
选择services.xml文件的位置,在Add工程目录下的src/resources/目录下,如上图所示。
选择输出文件的位置和文件名,这里我把输出文件放在 D:/ 目录下,取名为Add。如上图所示
选择Finish。则在D:/下可以找到一个名为Add.aar的文件。
将Add.aar文件拷贝到你的apache-tomact安装目录下的webapps/axis2/WEB-INF/services/目录下。
启动apache-tomact。
在浏览器中输入: http://192.168.1.235:8080/axis2/services
如有Add服务,则表示部署成功。如下图所示:
现在运行测试程序:
选择测试程序,Run As -> JUnit Test。
得到的正确结果应该是: 500
至此,使用myecplise、apache-tomact和axis2开发webservices的方法已经介绍完毕,读者可以根据自己的项目需求,按照以上流程操作,只需要修改业务代码,即xxSkeleton.java文件即可。
![](http://blogimg.chinaunix.net/blog/upfile2/080927223556.gif)
修改后为:
![](http://blogimg.chinaunix.net/blog/upfile2/080927223913.gif)
鼠标点击 File -> New -> Other,显示如下:
![](http://blogimg.chinaunix.net/blog/upfile2/080927224120.gif)
![](http://blogimg.chinaunix.net/blog/upfile2/080927224234.gif)
注意,这里与前面有所不同,应该选择Generate Java source code from a WSDL file。
然后选择next
![](http://blogimg.chinaunix.net/blog/upfile2/080927224502.gif)
点击Browse,选择wsdl文件的位置,然后点击next,首先生成服务代码,设置如下所示:
![](http://blogimg.chinaunix.net/blog/upfile2/080927224708.gif)
选择next
![](http://blogimg.chinaunix.net/blog/upfile2/080927225519.gif)
以下几步生成client测试代码,鼠标点击 File -> New -> Other
![](http://blogimg.chinaunix.net/blog/upfile2/080927225144.gif)
next
![](http://blogimg.chinaunix.net/blog/upfile2/080927225331.gif)
next
![](http://blogimg.chinaunix.net/blog/upfile2/080927225416.gif)
next
![](http://blogimg.chinaunix.net/blog/upfile2/080927225606.gif)
next
![](http://blogimg.chinaunix.net/blog/upfile2/080927225719.gif)
这时左边将显示如下,出现红X,说明有错误。
![](http://blogimg.chinaunix.net/blog/upfile2/080927225911.gif)
1、右键点击Add工程,选择Build Path ,再选择Add External Archives。
![](http://blogimg.chinaunix.net/blog/upfile2/080927230159.gif)
![](http://blogimg.chinaunix.net/blog/upfile2/080927230340.gif)
选择axis2包下的 WEB-INF/lib/目录下的所以 .jar文件。
2、将com.Add包下的Add.java拖动到src.com.Add包下,若出错选continue。然后删除com.Add包。
右键选择src.com.Add,选择Refactor,再选择Rename
![](http://blogimg.chinaunix.net/blog/upfile2/080927230955.gif)
修改后如下所示:
![](http://blogimg.chinaunix.net/blog/upfile2/080927231051.gif)
package test.com.add;
保存。
此时右键选择Add工程,选择Refresh,红X将消失。
![](http://blogimg.chinaunix.net/blog/upfile2/080927231442.gif)
5、编写业务代码。
双击com.add包下的文件AddSkeleton.java。
显示如下代码:
package com.add;
/**
* AddSkeleton java skeleton for the axisService
*/
public class AddSkeleton{
/**
* Auto generated method signature
* @param param0
*/
public com.add.xsd.AddResponse add
(
com.add.xsd.Add param0
)
{
//Todo fill this with the necessary business logic
throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#add");
}
}
修改后如下所示:
package com.add;
/**
* AddSkeleton java skeleton for the axisService
*/
public class AddSkeleton{
/**
* Auto generated method signature
* @param param0
*/
public com.add.xsd.AddResponse add
(
com.add.xsd.Add request
)
{
//Todo fill this with the necessary business logic
com.add.xsd.AddResponse response = new com.add.xsd.AddResponse();
int result = request.getInt1() + request.getInt2();
response.set_return(result);
return response;
}
}
保存。
然后编写测试代码:
双击test.com.add包下的AddTest.java文件
其中testadd()方法的代码显示如下:
public void testadd() throws java.lang.Exception{
com.add.AddStub stub =
new com.add.AddStub();//the default implementation should point to the right endpoint
com.add.AddStub.Add param4=
(com.add.AddStub.Add)getTestObject(com.add.AddStub.Add.class);
// todo Fill in the param4 here
assertNotNull(stub.add(
param4));
}
修改为:
public void testadd() throws java.lang.Exception{
java.lang.String url = "http://192.168.1.235:8080/axis2/services/Add";
com.add.AddStub stub =
new com.add.AddStub(url);//the default implementation should point to the right endpoint
com.add.AddStub.Add request=
(com.add.AddStub.Add)getTestObject(com.add.AddStub.Add.class);
// todo Fill in the param4 here
//在request中设置了两个数字,200和300,则返回结果应该是500
request.setInt1(200);
request.setInt2(300);
System.out.println(stub.add(request).get_return()); //结果为500
assertNotNull(stub.add(
request));
}
6、调试程序。
点击 File -> New -> Other
![](http://blogimg.chinaunix.net/blog/upfile2/080928160844.gif)
选择Axis2 Service Archiver,如上图所示,这一步是生成.aar的部署文件,选择next
![](http://blogimg.chinaunix.net/blog/upfile2/080928161000.gif)
点击Browse,选择class文件的位置,在Add工程目录下的WebRoot目录下的WEB-INF下的Class文件中。
next
![](http://blogimg.chinaunix.net/blog/upfile2/080928161152.gif)
选择WSDL,文件的位置,如上图所示。
![](http://blogimg.chinaunix.net/blog/upfile2/080928161252.gif)
直接点next,如上图所示
![](http://blogimg.chinaunix.net/blog/upfile2/080928161320.gif)
选择services.xml文件的位置,在Add工程目录下的src/resources/目录下,如上图所示。
![](http://blogimg.chinaunix.net/blog/upfile2/080928161427.gif)
选择输出文件的位置和文件名,这里我把输出文件放在 D:/ 目录下,取名为Add。如上图所示
选择Finish。则在D:/下可以找到一个名为Add.aar的文件。
将Add.aar文件拷贝到你的apache-tomact安装目录下的webapps/axis2/WEB-INF/services/目录下。
启动apache-tomact。
在浏览器中输入: http://192.168.1.235:8080/axis2/services
如有Add服务,则表示部署成功。如下图所示:
![](http://blogimg.chinaunix.net/blog/upfile2/080928162322.gif)
现在运行测试程序:
选择测试程序,Run As -> JUnit Test。
![](http://blogimg.chinaunix.net/blog/upfile2/080928162604.gif)
得到的正确结果应该是: 500
![](http://blogimg.chinaunix.net/blog/upfile2/080928163305.gif)
至此,使用myecplise、apache-tomact和axis2开发webservices的方法已经介绍完毕,读者可以根据自己的项目需求,按照以上流程操作,只需要修改业务代码,即xxSkeleton.java文件即可。