java学习:调用 java web service

先写一个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']:AwbModel


public  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();
            }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏GreenLeaves

C# 字符串操作基本过程(Equals、Compare、EndsWith等处理方法)

41320
来自专栏二进制文集

JSON Java 解析

要使程序可以运行必须引入JSON-lib包——org.json.jar包。综合来看,这个JAR包比较好用。

17320
来自专栏菩提树下的杨过

Silverlight与WPF中BeginInvoke的差异

Silverlight/WPF中,如果要在多线程中对界面控件值做修改,用Dispatcher对象的BeginInvoke方法无疑是最方便的办法 ,见:温故而知新...

23280
来自专栏程序员的SOD蜜

使用反射+缓存+委托,实现一个不同对象之间同名同类型属性值的快速拷贝

最近实践一个DDD项目,在领域层与持久层之间,Domain Model与Entity Model之间有时候需要进行属性值得拷贝,而这些属性,尽管它所在的类名称不...

22790
来自专栏技术博客

Dynamic 动态类型 和双问号??的使用

1.dynamic关键字用于声明一个动态对象,然后通过该动态对象去调用方法或读写属性。以前我们都是在运行时通过反射,Emit,CodeDom等技术来完成。创建一...

22220
来自专栏landv

从零开始学习C#——HelloWorld(一)

14750
来自专栏大内老A

由for V.S. for each想到的

一直想写一系列如何提高Performance和Scalability的文章,把我的相关经验和所知道的相关的技巧同大家分享。前一阵在园子里有一篇讨论for eac...

19280
来自专栏技术博客

Asp.net MVC后台 XML、DataTable、DataSet之间的数据转换

  上面的方法只是将XMl字符串读入到DataSet中,然后再冲DataSet中查找先前定义过的DataTable即可。

14420
来自专栏Java后端技术

22中编程语言的HelloWorld

8010
来自专栏码农阿宇

关于C#委托的一些学习笔记

1.什么是委托就是把方法作为参数传给另一个方法。委托说指向的函数,必须和函数具有相同的签名(返回值和参数类型) Public delegate void D...

27770

扫码关注云+社区

领取腾讯云代金券