跟我学WEBSERVICE(三)

来源:互联网 发布:网页ui设计软件 编辑:程序博客网 时间:2024/04/28 07:54

(http://blog.itpub.net/157586/viewspace-785265/)


作为程序员需要做哪些事情。

上面说了这么多,那么,做为程序员我们需要做哪些事情呢?我想以下事情是我们需要做的。我想下列事情是我们需要做的(我们举一个列子来说明问题):

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

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.<span times="" new="" roman"""="" style="word-wrap: break-word;">  客户端程序:

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.  将些文件生成一个JAR包。然后把这个JAR包放到LIB目录下。启动TOMCAT

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

注意:或许你现在很是迷惑,为什么不需要去修改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.AXIS自带的序列化和反序列化。不过,写这个序列化和反序列化程序是比较困难的。

0 0