Web Service (Axis) =======陈开源(albertchen79@126.com)

  定义接口(IPerson

package com.unimas.datacollection.webservices.training.api;

/**

 *这是一个描述人信息的接口,通过接口我们可以得到如下信息:名字,性别,身高,年龄。

 */

public interface IPerson {

    public String getName();

    public String getSex();

    public String toString();

    public int getAge();

    public int getTall();

}

 

package com.unimas.datacollection.webservices.training.api;

public interface IWs {

    public IPerson getPersonInfo(String name,int age,int tall,boolean sex);

}

 

2.  服务端程序的实现(实现接口):

package com.unimas.datacollection.webservices.training.server;

import com.unimas.datacollection.webservices.training.api.IPerson;

public class PersonImpl implements IPerson{

    private String m_name = null;

    private boolean m_sex = true;

    private int m_age = 0;

    private int m_tall = 0;

    public String getName() {

        return m_name;

    }

    public boolean getSex() {

        return m_sex;

    }

    public int getAge() {

        return m_age;

    }

    public int getTall() {

        return m_tall;

    }

    public void setSex(boolean isMan) {

        m_sex = isMan;

    }

    public void setName(String name) {

        m_name = name;

    }

    public void setAge(int age) {

        m_age = age;

    }

    public void setTall(int tall) {

        m_tall = tall;

    }

}

 

 

 

 

 

package com.unimas.datacollection.webservices.training.server;

import com.unimas.datacollection.webservices.training.api.IWs;

import com.unimas.datacollection.webservices.training.api.IPerson;

public class WsPersonServer implements IWs{

    public IPerson getPersonInfo(String name, int age, int tall, boolean sex) {

        PersonImpl person = new PersonImpl();

        person.setAge(age);

        person.setName(name);

        person.setSex(sex);

        person.setTall(tall);

        return person; 

    }

}

3.  客户端程序:

WsPersonClient类:

package com.unimas.datacollection.webservices.training.client;

import com.unimas.datacollection.webservices.training.api.IWs;

import com.unimas.datacollection.webservices.training.api.IPerson;

import com.unimas.datacollection.webservices.training.server.PersonImpl;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

public class WsPersonClient implements IWs{

private static final String Str_EndPoint =

"http://localhost:8080/axis/services/WsPersonServer";

    private static final String s_serviceName = "WsPersonServer";

    public WsPersonClient() {

    }

    public IPerson getPersonInfo(String name, int age, int tall, boolean sex) {

        Service service = new Service();

        Call call = null;

        IPerson person = null;

        try {

            call = (Call) service.createCall();

            call.setTargetEndpointAddress( new java.net.URL(Str_EndPoint));

            QName qname = new QName(s_serviceName, "getPersonInfo");

            call.setOperationName(qname);

            call.addParameter("name", qname, ParameterMode.IN);

            call.addParameter("age", qname, ParameterMode.IN);

            call.addParameter("tall", qname, ParameterMode.IN);

            call.addParameter("sex", qname, ParameterMode.IN);

            call.setReturnClass(IPerson.class);

            call.setReturnType(qname, IPerson.class);

 

            QName qn = new QName("urn:WsPersonServer", "Person");

            call.registerTypeMapping(PersonImpl.class, qn,

        new org.apache.axis.encoding.ser.BeanSerializerFactory (PersonImpl.class,qn),

        new org.apache.axis.encoding.ser.BeanDeserializerFactory(PersonImpl.class,qn));

person = (IPerson)call.invoke(new Object[]{name,

new Integer(age),

new Integer(tall),

new Boolean(sex)});

        } catch(Exception e) {

            e.printStackTrace();

        }

        return person;

    }

}

测试类:TestClient

package com.unimas.datacollection.webservices.training.client;

 

import com.unimas.datacollection.webservices.training.api.IPerson;

import com.unimas.datacollection.webservices.training.Servcie;

public class TestClient {

public static void main(String[] args) {

        Servcie.deploy("C:/dev_myself/webServices/training/src/resources/deploy.personService.wsdd");

        WsPersonClient wsClient = new WsPersonClient();

        IPerson person = wsClient.getPersonInfo("syc",24,170,true);

        System.out.println("Your Name     is:"+person.getName());

        System.out.println("Your Age      is:"+person.getAge());

        System.out.println("Your Stature is:"+person.getTall());

        boolean isMan = person.getSex();

        if(isMan) {

            System.out.println("You are a man!");

        } else {

            System.out.println("You are a women!");

        }

    }

}

Servcie类:

public class Servcie {

    public static void deploy(String deployFile) {

        AdminClient.main(new String[]{deployFile});

    }

    public static void undeploy(String undeployFile) {

        AdminClient.main(new String[]{undeployFile});

    }

}

4.  部署:编写deploy.personService.wsdd文件。内容如下:

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

    <service name="WsPersonServer" provider="java:RPC">

        <parameter name="allowedMethods" value="*"/>

        <parameter name="className" value="com.unimas.datacollection.webservices.training.server.WsPersonServer"/>

        <beanMapping qname="myNS:Person" xmlns:myNS="urn:WsPersonServer" languageSpecificType="java:com.unimas.datacollection.webservices.training.server.PersonImpl"/>

    </service>

</deployment>

并把它存放到某一个目录。

5.  将些文件生成一个JAR包。然后把这个JAR包放到LIB目录下。启动TOMCAT

6.  先用IE访问服务,看服务是否已经启动:

http://localhost:8080/axis/services/WsPersonServer。如果没有异常,那么就表示部署成功了。

7.  启动测试程序测试客户端。

注意:或许你现在很是迷惑,为什么不需要去修改Server-config.wsdd文件了。其实,这里我们已经修改了这个文件,已经把我们需要的服务部署到这个文件中了。就是下面语句代替了我们手工的工作:

Servcie.deploy("C:/dev_myself/webServices/training/src/resources/deploy.personService.wsdd");

程序开发中需要注意的问题。

 

在使用AXIS开发WEB服务的时候,会遇到很多问题。比如:XML解析器出现的异常、发布Web服务时报告"Exception:: (404)Not Found"、客户端程序找不到可用的Web服务、序列化/反序列化等。当然,在开发中还会有更多的问题出现,下面这个网址介绍了对问题的情况描述和问题的解答。

 

http://www-900.ibm.com/developerWorks/cn/webservices/ws-axisfaq/index.shtml

关于序列化和反序列化的问题,可以参阅:

 

http://it.icxo.com/htmlnews/2004/09/29/386559.htm

 

目前还没有解决的问题。

 

1.  WEBSERVICE之间好象不能传递父类的信息:不管是合成的还是继承的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值