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

          跟我學WEBSERVICE(三)

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

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

          1. ? 定義接口( 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 之間好象不能傳遞父類的信息:不管是合成的還是繼承的。

          現在公司開發使用的序列化好象都是 AXIS 自帶的序列化和反序列化。不過,寫這個序列化和反序列化程序是比較困難的。

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

          主站蜘蛛池模板: 分宜县| 文成县| 安图县| 吴忠市| 浦北县| 巴南区| 渭南市| 赤水市| 通山县| 乐业县| 原平市| 玛纳斯县| 敦煌市| 天全县| 根河市| 赤壁市| 高邑县| 建瓯市| 宜宾县| 东宁县| 博野县| 安龙县| 庆城县| 西乌珠穆沁旗| 太谷县| 沭阳县| 从化市| 贵港市| 南郑县| 浪卡子县| 黄大仙区| 克什克腾旗| 寿宁县| 建德市| 高尔夫| 洛扎县| 内江市| 桂阳县| 克东县| 紫云| 金阳县|