1.去http://axis.apache.org/axis2/java/core/download.cgi 下载axis2 1.41 Binary Distribution
2.配置axis2的环境变量
3.在eclipse中配置axis2路径
开发Web Service:
一、新建一个Java Project,命名为"axis2Study";
二·、新建一个po类,命名为"Student",完整代码如下:
package com.nj.po; public class Student { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + "]"; } }
三.新建一个服务类 StudentManager :
package com.nj.server; import com.nj.po.Student; public class StudentManager { public RtnMeg addStudent(Student stu){ RtnMeg meg=new RtnMeg(); meg.setRtnCode(0); meg.setRtnMeg("success"); System.out.println(stu.toString()); return meg; } }
3、在"WS_01"项目上new --> other,找到"Web Services"下面的"Web Service";
4、下一步(next),在出现的Web Services对象框,在Service implementation中点击"Browse",进入
Browse Classes对象框,查找到我们刚才写的写的StudentManager类。(如下图)。点击"ok",则回到
Web Service话框。
5、在Web Service对话框中,将Web Service type中的滑块,调到"start service“的位置,将Client
type中的滑块调到"Test client"的位置。
6、在Web Service type滑块图的右边有个"Configuration",点击它下面的选项,进入Service
Deployment Configuration对象框,在这里选择相应的Server(我这里用Tomcat6.0)和Web Service
runtime(选择Apache Axis2),如下图:
7、点OK后,则返回到Web Service对话框,同理,Client type中的滑块右边也有"Configuration",也
要进行相应的置,步骤同上。完成后,Next --> next即行。
8、到了Server startup对话框,有个按键"start server"(如下图),点击它,则可启动Tomcat服务器
9、等启完后,点击"next -- > next",一切默认即行,最后,点击完成。最后,出现如下界面:(Web
Service Explorer),我们在这里便可测试我们的Web服务。
至此http://127.0.0.1/axis2Study/services/StudentManager?wsdl 已经能访问了,同时客户端,服务端代码都已生成
10.在axis2StudyClient项目也创建一个com.nj.po包,创建一个同样的Student类,这一步重要,否则后面会报错
package com.nj.po; public class Student { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + "]"; } }
11.测试客户端
package com.nj.client; import java.rmi.RemoteException; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import com.nj.po.Student; import com.nj.server.StudentManagerStub; public class TestClient { public static void main(String[] args) throws RemoteException { System.out.println("*****************************stub*****************************"); stub(); System.out.println("*****************************rpc*****************************"); rpc(); } public static void stub() throws RemoteException{ StudentManagerStub stub = new StudentManagerStub("http://127.0.0.1/axis2Study/services/StudentManager"); // 创建Student对象 StudentManagerStub.Student Student = new StudentManagerStub.Student(); Student.setAge("23"); Student.setName("张三"); // 设置要调用的方法的值 StudentManagerStub.AddStudent addStudent = new StudentManagerStub.AddStudent(); addStudent.setStu(Student); // 调用getStudent并得到相应的返回值 StudentManagerStub.AddStudentResponse response = stub.addStudent(addStudent); System.out.println("age="+ response.get_return().getAge()); System.out.println("name="+ response.get_return().getName()); } public static void rpc() throws AxisFault{ RPCServiceClient client = new RPCServiceClient(); Options option = client.getOptions(); // 指定客户端访问的webservice服务器端地址 EndpointReference erf = new EndpointReference("http://127.0.0.1/axis2Study/services/StudentManager"); option.setTo(erf); // 指定命名空间,指定要调用的方法 QName name = new QName("http://server.nj.com","addStudent"); // 创建Student对象 Student Student = new Student(); Student.setAge("20"); Student.setName("张三"); // 创建要传送的object数组 Object[] object = new Object[]{Student}; // 创建返回的参数类型 Class[] returnTypes = new Class[]{Student.class}; // 调用远程服务,得到返回的object数组 Object[] response = client.invokeBlocking(name, object, returnTypes); // 强制转换成Student对象 Student stu = (Student)response[0]; System.out.println(stu.toString()); } }