1.下载①axis2-1.7.4-bin.zip、②axis2-1.7.4-war.zip、③axis2-eclipse-service-plugin-1.7.4.zip、④axis2-eclipse-codegen-plugin-1.7.4.zip(下载地址:http://axis.apache.org/axis2/java/core/download.cgi);
2.将②axis2-1.7.4-war.zip解压后得到的axis2.war文件放到Tomcat下的webapps目录下,重启Tomcat访问http://localhost:8080/axis2/可看到内容;
3.将③axis2-eclipse-service-plugin-1.7.4.zip、④axis2-eclipse-codegen-plugin-1.7.4.zip解压后得到的插件放到Myeclipse的dropins目录下,重启Myeclipse生效;
4.新建web project,名为Axis2Service,新建student类:
package cn.lxc.bean; import java.io.Serializable; public class Student implements Serializable{ private static final long serialVersionUID = 4634408543522196927L; private int id; private int number; private String name; private String tel; private String address; public Student() { } public Student(int id, int number, String name, String tel, String address) { this.id = id; this.number = number; this.name = name; this.tel = tel; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
创建MyService类,用于发布webservice服务:
package cn.lxc.service; import java.util.Arrays; import cn.lxc.bean.Student; public class MyService { public String getGreeting(String name){ return "您好,"+name+"!"; } public String addStudent(Student stu){ if(null != stu) return "您好,"+stu.getName()+"!"; else return "student is null!"; } public Student queryStudent(){ Student stu = new Student(111111, 20170302, "刘新成", "18810464513", "北京"); return stu; } public String addStudents(Student[] students){ if(null != students) return Arrays.toString(students); else return "students is null"; } public Student[] queryStudents(){ Student[] stuArr = new Student[2]; Student st1 = new Student(222222, 20170301, "刘德华", "13893260374", "香港"); Student st2 = new Student(111111, 20170302, "刘新成", "18810464513", "北京"); stuArr[0] = st1; stuArr[1] = st2; return stuArr; } }
5.发布webservice:
(1)右击项目名,Axis2Service——>New——>Other——>Axis2 Service Archiver;
(2)Class File Location路径:项目WEB-INF下的classes目录(例如:E:WorkspacesAxis2ServicewebWEB-INFclasses);
(3)勾选Skip WSDL,点击下一步;
(4)Add any external libraries忽略,下一步;
(5)勾选Generator the service xml automatically,下一步;
(6)Service name:填写要发布的service名称,Class name:要发布的类的全称(例如:cn.lxc.service.MyService),下一步;
(7)Output file location:指定要发布的service到tomcat的axis2项目容器中(例如:C:apache-tomcat-7.0.69webappsaxis2WEB-INFservices);点击finish即可;
(8)启动tomcat,访问http://localhost:8080/axis2/services/listServices,即可看到新发布的service:MyService,点击MyService链接,得到wsdl文件。
6.使用客户端调用webservice:
(1)新建web project,命名为Axis2Client,将①axis2-1.7.4-bin.zip解压后的包下的lib目录下的jar包添加到项目web/WEB-INF/lib目录下;
(2)使用myeclipse的axis2插件Axis2 Code Generator生成客户端;
(3)右击项目Axis2Service,New——>Other——>Axis2 Code Generator,点击下一步;
(4)选择Generator Java source code from a WSDL file,点击下一步;
(5)WSDL file location:路径指向点击MyService链接得到的wsdl文件(例如:C:UsersliuxinchengDesktopMyService.xml),点击下一步;
(6)Codegen option:默认default,选择custom(定制),其他默认,点击下一步;
(7)选择Browse and select a project on current eclipse workspace,Output Path:选择Axis2Client的路径,例如(E:WorkspacesAxis2Client),点击finish即可;
(8)创建Test类测试:
package cn.lxc.test; import cn.lxc.bean.xsd.Student; import cn.lxc.service.AddStudent; import cn.lxc.service.AddStudentResponse; import cn.lxc.service.AddStudents; import cn.lxc.service.AddStudentsResponse; import cn.lxc.service.GetGreeting; import cn.lxc.service.GetGreetingResponse; import cn.lxc.service.MyServiceStub; import cn.lxc.service.QueryStudent; import cn.lxc.service.QueryStudentResponse; import cn.lxc.service.QueryStudents; import cn.lxc.service.QueryStudentsResponse; public class Test { public static void main(String[] args){ // 设置远程服务调用地址 String target = "http://127.0.0.1:8080/axis2/services/MyService"; try{ // 根据地址构造用户存根 MyServiceStub stub = new MyServiceStub(target); // 1. 调用getGreeting方法,设置入参对象及属性,入参,回参为基本类型String GetGreeting getGreeting = new GetGreeting(); getGreeting.setName("Axis");//设置方法参数 GetGreetingResponse ggr = stub.getGreeting(getGreeting); // 接收方法返回值 String getResult = ggr.get_return(); System.out.println(getResult); // 2. 调用AddStudent方法,设置入参对象及属性,入参为bean AddStudent addStudent = new AddStudent(); Student student = new Student(); student.setAddress("shanghai"); student.setId(123456); student.setName("张三"); student.setNumber(12321312); student.setTel("13678956529"); addStudent.setStu(student); AddStudentResponse asr = stub.addStudent(addStudent); // 接收方法返回值 String addResult = asr.get_return(); System.out.println(addResult); // 3. 调用queryStudent方法,设置入参对象及属性,回参为bean QueryStudent queryStudent = new QueryStudent(); QueryStudentResponse qsr = stub.queryStudent(queryStudent); // 接收方法返回值 Student qsResult = qsr.get_return(); System.out.println("地址:" + qsResult.getAddress() + ", ID:" + qsResult.getId() + ", 姓名:" + qsResult.getName() + ", 号码:" + qsResult.getNumber() + ", 电话:" + qsResult.getTel()); System.out.println("<------------------------------------------------------>"); // 4. 调用queryStudents方法,设置入参对象及属性,返回bean数组 QueryStudents queryStudents = new QueryStudents(); QueryStudentsResponse qssr = stub.queryStudents(queryStudents); // 接收方法返回值 Student[] qssResult = qssr.get_return(); for (int i = 0; i < qssResult.length; i++){ System.out.println("地址:" + qssResult[i].getAddress() + ", ID:" + qssResult[i].getId() + ", 姓名:" + qssResult[i].getName() + ", 号码:" + qssResult[i].getNumber() + ", 电话:" + qssResult[i].getTel()); } // 5. 调用AddStudents方法,设置入参对象及属性,入参为bean数组 AddStudents addStudents = new AddStudents(); Student st = new Student(); st.setAddress("shanghai"); st.setId(123456); st.setName("servyou"); st.setNumber(12321312); st.setTel("13678956529"); Student st2 = new Student(); st2.setAddress("shanghai"); st2.setId(123456); st2.setName("servyou"); st2.setNumber(12321312); st2.setTel("13678956529"); addStudents.addStudents(st); addStudents.addStudents(st2); AddStudentsResponse assr = stub.addStudents(addStudents); // 接收方法返回值 String addstsResult = assr.get_return(); System.out.println(addstsResult); } catch (Exception e){ e.printStackTrace(); } } }