首页 > 代码库 > Axis2开发实例

Axis2开发实例

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:\Workspaces\Axis2Service\web\WEB-INF\classes);

(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.69\webapps\axis2\WEB-INF\services);点击finish即可;

(8)启动tomcat,访问http://localhost:8080/axis2/services/listServices,即可看到新发布的service:MyService,点击MyService链接,得到wsdl文件。

Axis2开发实例