第一步:下载eclipse-helios-release2,下载地址:http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR2/eclipse-jee-helios-SR2-win32.zip
解压后直接可用。
第二步:下载并安装tomcat插件
http://www.fayea.com/apache-mirror/tomcat/tomcat-6/v6.0.35/bin/apache-tomcat-6.0.35.zip
eclipse tomcat插件:http://www.eclipsetotale.com/tomcatPlugin/tomcatPluginV33.zip
解压tomcatPluginV33.zip,将解压后的文件放到eclipse/plugins内,重启eclipse,可看见tomcat图标,在window/perference 下,
选择tomcat,tomcat version里选择version 6.x,tomcat home 中选择tomcat 的安装目录。按tomcat启动图标,
在浏览器里输入http://localhost:8080/,可以看见官网。说明已经成功了。
第三步:下载axis2
http://apache.etoak.com//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip
http://apache.etoak.com//axis/axis2/java/core/1.6.2/axis2-1.6.2-war.zip
将axis2-1.6.2-bin.zip解压到电脑中的任意目录,
将axis2-1.6.2-war.zip解压后,放到tomcat的webapps目录下,重启tomcat,会发现webapps目录下多了axis2文件夹,
在浏览器里输入http://localhost:8080/axis2,会发现对应的网页,说明已经成功了。
在Eclipse中进行配置,会出现Axis2 runtimeloaded successfully,说明加载成功了。再下载两个插件:http://apache.etoak.com/axis/axis2/java/core/1.6.2/axis2-eclipse-codegen-plugin-1.6.2.zip
http://mirror.bit.edu.cn/apache/axis/axis2/java/core/1.6.2/axis2-eclipse-service-plugin-1.6.2.zip
解压后都放到eclipse的plugins目录下,在eclipse中,file-->new-->others..里面会出现axis2 wizards。
这样,axis2 配置完成了,可以进行web service开发了......
第四步:编写服务代码
创建Java工程,File-->New-->Project-->JavaProject
创建Class,添加代码:
import java.util.Random;
public class HelloWorldService {
public String sayHello(String name) {
return name + " say: hello[axis2]";
}
public int getAge(int i) {
return i + new Random().nextInt(100);
}
}
第五步、打包和部署服务
File-->New-->Other-->Axis2Wizards-->Axis2 Service Archiver,步骤如下:
先Next,然后出现如下图所示的对话框,输入class的文件路径
然后Next,选择Skip WSDL;Next,Next,Next,填写服务与类名。备注:类名一定要是全路径,即包.类。
我用的是默认的包,就不用在前面加包名,如下图
Next,指定输出文件的路径与名字,路径选择tomcat\webapps\axis2\WEB-INF\services,名字我填的是MyService,然后Finish。到此,一个服务的arr文件已经生成。重启tomcat。
访问http://localhost:8080/axis2/ ,点击Service,将看到部署的服务。
第六步:调用服务
1、生成客户端stub代码
File—New—Other—Axis2 Wizards—Axis2 CodeGenerator,步骤如下:
1)选择Next,默认选择Generate Java source code from a WSDLfile(根据WSDL生成webservice客户端的java代码):
2)Next,输入WSDL路径:注:WSDL的路径是单击MyService所产生的URL地址,我的是http://localhost:8080/axis2/services/MyService?wsdl
3)Next—Next,指定生成的代码放置的工程,可以选择当前工作空间中已有的工程,下图中Client即是之前已经在当前工作空间中创建的Java Project:
4)Finish,代码生成完成,此时Client中自动生成几个文件,如下:
2、添加JAR包
右键Client—Build Path—Configure BuildPath—Libraries—Add External JARs,添加之前解压的axis2-1.6.2(由axis2-1.6.2-bin.zip解压而来)文件夹下lib文件夹中所有的JAR文件
3、编写调用代码
右键Client—New Class,在“Which method stubs would you like tocreate?”中勾选“public static void main(String[] args)”,类中代码如下:
package org.apache.ws.axis2;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
public class CallTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyService helloService;
// new一个调用sayHello方法需要的参数SayHello,并且设置name
SayHello sayHello = new SayHello();
sayHello.setArgs0("xaut");
GetAge getAge = new GetAge();
getAge.setArgs0(30);
// 调用web服务
SayHelloResponse sayHelloResponse;
try {
helloService = new MyServiceStub();
sayHelloResponse =helloService.sayHello(sayHello);
System.out.println(sayHelloResponse.get_return());
GetAgeResponse getAgeResponse =helloService.getAge(getAge);
System.out.println(getAgeResponse.get_return());
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}