隨筆 - 9, 文章 - 1, 評論 - 2, 引用 - 0
          數(shù)據(jù)加載中……

          跟我學(xué)WEBSERVICE(三)

          作為程序員需要做哪些事情。

          上面說了這么多,那么,做為程序員我們需要做哪些事情呢?我想以下事情是我們需要做的。我想下列事情是我們需要做的(我們舉一個(gè)列子來說明問題):

          1. ? 定義接口( IPerson

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

          /**

          ?* 這是一個(gè)描述人信息的接口,通過接口我們可以得到如下信息:名字,性別,身高,年齡。

          ?*/

          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. ? 服務(wù)端程序的實(shí)現(xiàn)(實(shí)現(xiàn)接口):

          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 文件。內(nèi)容如下:

          <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>

          并把它存放到某一個(gè)目錄。

          5. ? 將些文件生成一個(gè) JAR 包。然后把這個(gè) JAR 包放到 LIB 目錄下。啟動 TOMCAT

          6. ? 先用 IE 訪問服務(wù),看服務(wù)是否已經(jīng)啟動:

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

          7. ? 啟動測試程序測試客戶端。

          注意:或許你現(xiàn)在很是迷惑,為什么不需要去修改 Server-config.wsdd 文件了。其實(shí),這里我們已經(jīng)修改了這個(gè)文件,已經(jīng)把我們需要的服務(wù)部署到這個(gè)文件中了。就是下面語句代替了我們手工的工作:

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

          程序開發(fā)中需要注意的問題。

          在使用 AXIS 開發(fā) WEB 服務(wù)的時(shí)候,會遇到很多問題。比如: XML 解析器出現(xiàn)的異常、 發(fā)布 Web 服務(wù)時(shí)報(bào)告 "Exception:: (404)Not Found" 、客戶端程序找不到可用的 Web 服務(wù)、 序列化 / 反序列化等。當(dāng)然,在開發(fā)中還會有更多的問題出現(xiàn),下面這個(gè)網(wǎng)址介紹了對問題的情況描述和問題的解答。

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

          關(guān)于序列化和反序列化的問題,可以參閱:

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

          ?

          目前還沒有解決的問題。

          1. ? WEBSERVICE 之間好象不能傳遞父類的信息:不管是合成的還是繼承的。

          現(xiàn)在公司開發(fā)使用的序列化好象都是 AXIS 自帶的序列化和反序列化。不過,寫這個(gè)序列化和反序列化程序是比較困難的。

          posted on 2007-03-24 16:03 趙貴陽 閱讀(587) 評論(0)  編輯  收藏 所屬分類: WEBSERVICE

          主站蜘蛛池模板: 囊谦县| 阳城县| 巨野县| 桂平市| 南城县| 公安县| 蓬安县| 宁陵县| 政和县| 新建县| 塘沽区| 天祝| 上饶市| 清涧县| 中江县| 鸡西市| 皮山县| 嘉兴市| 易门县| 维西| 兰溪市| 利川市| 潼关县| 徐汇区| 津市市| 泸州市| 永昌县| 金平| 巴马| 新和县| 麟游县| 米泉市| 古浪县| 睢宁县| 荣成市| 获嘉县| 永年县| 独山县| 缙云县| 玛沁县| 兴国县|