java學習:調用 java web service

來源:互聯網
上載者:User

先寫一個java的class:AwbModel(相當於要在web service中傳輸的實體物件)

package webservicesample;public class AwbModel {    private String _a1 = "_a1";    public String getA1() {        return _a1;    }    public void setA1(String value) {        this._a1 = value;    }    private String _A2 = "_A2";    public String getA2() {        return _A2;    }    public void setA2(String value) {        this._A2 = value;    }    private String a3 = "a3";    public String getA3() {        return a3;    }    public void setA3(String value) {        this.a3 = value;    }    private String A4 = "A4";    public String getA4() {        return A4;    }    public void setA4(String value) {        this.A4 = value;    }    private String A5 = "A5";    public String getA5() {        return A5;    }    private String A6 = "A6";    public void setA6(String value) {        this.A6 = value;    }    public String A7 = "A7";    //private String _a8="a8";    public String get_a8(){        //return _a8;        return "";    }    public void set_a8(String value){        //this._a8=value;    }    public String sayHelloA(){        return "Hello!";    }}

  然後再寫一個Awb類(相當於web service的介面+實現)

package webservicesample;public class Awb {    public AwbModel GetAwbModelByNo() {        AwbModel a = new AwbModel();        a.setA1("a1_new");        return a;    }    private String _b1 = "_b1";    public String getB1() {        return _b1;    }    public void setB1(String value) {        this._b1 = value;    }    private String _B2 = "_B2";    public String getB2() {        return _B2;    }    public void setB2(String value) {        this._B2 = value;    }    private String b3 = "b3";    public String getB3() {        return b3;    }    public void setB3(String value) {        this.b3 = value;    }    private String B4 = "B4";    public String getB4() {        return B4;    }    public void setB4(String value) {        this.B4 = value;    }    private String B5 = "B5";    public String getB5() {        return B5;    }    private String B6 = "B6";    public void setB6(String value) {        this.B6 = value;    }    public String B7 = "B7";    public String sayHelloB() {        return "Hello B!";    }}

 jBuilder2006環境下,封裝成web service發布到weblogic後,WSDL的內容見:

 

注意:因為A5隻有get方法,A6隻有set方法,最終產生的WSDL自動去掉了這二個“屬性”(後面在用c#調用該java web Service時,能看到這將會產生真正意義上的.net property),自訂的其它方法(比如sayHelloA)被丟棄(即:這類方法沒有自動序列化),同時比較有意思的是"_a8",AwbModel.java裡只定義了set_a8(String value),get_a8()方法,根本沒有聲明_a8的private變數,但是java自動視為已經存在“_a8”的私人成員;此外直接聲明的public變數,WSDL裡不改變大小寫,仍然還是A7;而其它的setXXX,getXXX方法,最終一律去掉set/get後,將首字母小寫,即xXX。

類似vs.net裡加入服務參考後,會自動產生xxxClient、xxxChannel、代理類等用戶端調用代碼一樣,make之後,jBuilder也會產生用戶端程式碼範例包Awb_Client.jar,解壓後看下AwbModel.java的內容:

/** * This code was automatically generated at 5:48:57 on 2012-12-4 * by weblogic.xml.schema.binding.internal.codegen.BeanImplGenerator -- do not edit. * * @version WebLogic Server 9.2 SP0  Mon Jul 31 05:48:10 PDT 2006 796124  * @author Copyright (c) 2012 by BEA Systems, Inc. All Rights Reserved. */package webservicesample.generated;// original type: ['java:webservicesample']:AwbModelpublic  class AwbModel     implements java.io.Serializable{  public AwbModel() {}  public AwbModel(java.lang.String p__A8,     java.lang.String p_a1,     java.lang.String p_a2,     java.lang.String p_a3,     java.lang.String p_a4,     java.lang.String p_a7)   {     this._A8 = p__A8;          this.a1 = p_a1;          this.a2 = p_a2;          this.a3 = p_a3;          this.a4 = p_a4;          this.a7 = p_a7;       }  private java.lang.String _A8 ;  /**  <br>  Derived from _a8.  <br>  schema name = ['java:webservicesample']:_a8  <br>  schema type = ['http://www.w3.org/2001/XMLSchema']:string  <br>  schema formQualified = true  */  public java.lang.String get_A8() {    return _A8;  }    public void set_A8(java.lang.String v) {        this._A8 = v;      }    private java.lang.String a1 ;  /**  <br>  Derived from a1.  <br>  schema name = ['java:webservicesample']:a1  <br>  schema type = ['http://www.w3.org/2001/XMLSchema']:string  <br>  schema formQualified = true  */  public java.lang.String getA1() {    return a1;  }    public void setA1(java.lang.String v) {        this.a1 = v;      }    private java.lang.String a2 ;  /**  <br>  Derived from a2.  <br>  schema name = ['java:webservicesample']:a2  <br>  schema type = ['http://www.w3.org/2001/XMLSchema']:string  <br>  schema formQualified = true  */  public java.lang.String getA2() {    return a2;  }    public void setA2(java.lang.String v) {        this.a2 = v;      }   private java.lang.String a3 ;  /**  <br>  Derived from a3.  <br>  schema name = ['java:webservicesample']:a3  <br>  schema type = ['http://www.w3.org/2001/XMLSchema']:string  <br>  schema formQualified = true  */  public java.lang.String getA3() {    return a3;  }    public void setA3(java.lang.String v) {        this.a3 = v;      }    private java.lang.String a4 ;  /**  <br>  Derived from a4.  <br>  schema name = ['java:webservicesample']:a4  <br>  schema type = ['http://www.w3.org/2001/XMLSchema']:string  <br>  schema formQualified = true  */  public java.lang.String getA4() {    return a4;  }    public void setA4(java.lang.String v) {        this.a4 = v;      }    private java.lang.String a7 ;  /**  <br>  Derived from A7.  <br>  schema name = ['java:webservicesample']:A7  <br>  schema type = ['http://www.w3.org/2001/XMLSchema']:string  <br>  schema formQualified = true  */  public java.lang.String getA7() {    return a7;  }    public void setA7(java.lang.String v) {        this.a7 = v;      }  public java.lang.String toString() {  return "AwbModel" + "{"             + " _A8=<" + get_A8() + ">"             + " a1=<" + getA1() + ">"             + " a2=<" + getA2() + ">"             + " a3=<" + getA3() + ">"             + " a4=<" + getA4() + ">"             + " a7=<" + getA7() + ">"    + " }";  }  public boolean equals(java.lang.Object __other__) {    if (__other__ == this) return true;    if (__other__ instanceof AwbModel) {            AwbModel __obj__ =  (AwbModel) __other__;      return true            && (_A8==null ? __obj__.get_A8()==null : _A8.equals(__obj__.get_A8()))            && (a1==null ? __obj__.getA1()==null : a1.equals(__obj__.getA1()))            && (a2==null ? __obj__.getA2()==null : a2.equals(__obj__.getA2()))            && (a3==null ? __obj__.getA3()==null : a3.equals(__obj__.getA3()))            && (a4==null ? __obj__.getA4()==null : a4.equals(__obj__.getA4()))            && (a7==null ? __obj__.getA7()==null : a7.equals(__obj__.getA7()))      ;    }    return false;  }  public int hashCode() {    int __hash__result__ = 17;    __hash__result__ = 37*__hash__result__ + (_A8==null ? 0 : _A8.hashCode()) ;    __hash__result__ = 37*__hash__result__ + (a1==null ? 0 : a1.hashCode()) ;    __hash__result__ = 37*__hash__result__ + (a2==null ? 0 : a2.hashCode()) ;    __hash__result__ = 37*__hash__result__ + (a3==null ? 0 : a3.hashCode()) ;    __hash__result__ = 37*__hash__result__ + (a4==null ? 0 : a4.hashCode()) ;    __hash__result__ = 37*__hash__result__ + (a7==null ? 0 : a7.hashCode()) ;        return __hash__result__;  }}

  

與WSDL裡看到的基本一致,不過跟原始的AwbModel.java相比,還是有一些有趣的小變化:原始AwbModel.java裡定義的私成成員,一律變成了小寫,而自動添加的"_A8"卻又是大寫。

下面看看用java如何調用web service:

隨便建立一個java項目,把jBuilder自動產生的Awb_client.jar包引用進來

package jmyang.web_service_call;import webservicesample.generated.*;import java.io.IOException;import javax.xml.rpc.ServiceException;public class wsCallDemo {    public void callService() {        String wsdlUrl = "http://127.0.0.1:7001/MyWebService/Awb?WSDL";        try {            Awb service = new Awb_Impl(wsdlUrl);            AwbPort port = service.getAwbPort();            AwbModel result = port.getAwbModelByNo();            System.out.println("awbmodel.a1=" + result.getA1());        } catch (IOException e) {            e.printStackTrace();        } catch (ServiceException e2) {            e2.printStackTrace();        }    }}

  

這樣就能調用了.

.net當然也能調用java寫的web service,在vs.net裡加入服務參考,服務地址輸入:http://127.0.0.1:7001/MyWebService/Awb?WSDL vs.net會象添加wcf/asmx服務引用一樣,自動組建代理程式類等其它代碼,看看自動產生的AwbModel類

 /// <remarks/>    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]    [System.SerializableAttribute()]    [System.Diagnostics.DebuggerStepThroughAttribute()]    [System.ComponentModel.DesignerCategoryAttribute("code")]    [System.Xml.Serialization.SoapTypeAttribute(Namespace="java:webservicesample")]    public partial class AwbModel : object, System.ComponentModel.INotifyPropertyChanged {                private string _a8Field;                private string a1Field;                private string a2Field;                private string a3Field;                private string a4Field;                private string a7Field;                /// <remarks/>        [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]        public string _a8 {            get {                return this._a8Field;            }            set {                this._a8Field = value;                this.RaisePropertyChanged("_a8");            }        }                /// <remarks/>        [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]        public string a1 {            get {                return this.a1Field;            }            set {                this.a1Field = value;                this.RaisePropertyChanged("a1");            }        }                /// <remarks/>        [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]        public string a2 {            get {                return this.a2Field;            }            set {                this.a2Field = value;                this.RaisePropertyChanged("a2");            }        }                /// <remarks/>        [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]        public string a3 {            get {                return this.a3Field;            }            set {                this.a3Field = value;                this.RaisePropertyChanged("a3");            }        }                /// <remarks/>        [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]        public string a4 {            get {                return this.a4Field;            }            set {                this.a4Field = value;                this.RaisePropertyChanged("a4");            }        }                /// <remarks/>        [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]        public string A7 {            get {                return this.a7Field;            }            set {                this.a7Field = value;                this.RaisePropertyChanged("A7");            }        }                public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;                protected void RaisePropertyChanged(string propertyName) {            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;            if ((propertyChanged != null)) {                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));            }        }    }

  

可以發現,剛才裡的a1、a2、a3、a4、A7、_a8全都以屬性(property)的方式出現了。

c#的調用代碼還是一如繼往的簡單:

            using (YJM.AwbPortClient client = new AwbPortClient())            {                var model = client.GetAwbModelByNo();                Console.WriteLine("a1=" + model.a1);                Console.WriteLine("a2=" + model.a2);                Console.WriteLine("a3=" + model.a3);                Console.WriteLine("a4=" + model.a4);                Console.WriteLine("A7=" + model.A7);                Console.WriteLine("_a8=" + model._a8);                Console.Read();            }

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.