星空网 > 软件开发 > ASP.net

wsdl 关于nillable和minOccurs 在.NET和java中的不同

术语约定
文章中会反复出现[值类型]、[包装类型]、[普通引用类型]、[元素节点]和[元素取值]的表述
1> [值类型]指的是java和.NET中的基本数据类型,如:int;
2> [包装类型]指的是java中的包装类和.NET中的Nullable<T>类型,如:Integer、int?;
3> [普通引用类型]指的是除包装类型外的其它引用类型;
4> [元素节点]指的是5> [元素取值]指的是

1、首先来看minOccurs和Nillable的定义
minOccurs
:表示nillable:表示那么,minOccurs和nillable的组合形式就会有4种,如下:
例一,值类型:

<element name="id1" type="int" minOccurs="0" nillable="true"/><element name="id2" type="int" minOccurs="0" nillable="false"/><element name="id3" type="int" minOccurs="1" nillable="true"/><element name="id4" type="int" minOccurs="1" nillable="false"/>

例二,引用类型:

<element name="name1" type="string" minOccurs="0" nillable="true"/><element name="name2" type="string" minOccurs="0" nillable="false"/><element name="name3" type="string" minOccurs="1" nillable="true"/><element name="name4" type="string" minOccurs="1" nillable="false"/>

2、Java和.NET自动生成WSDL的规则
在实际开发时我们通常的做法是先编写Server端代码,然后利用工具自动生成wsdl描述,最后再利用工具读取wsdl进而生成客户端程序。那么用工具生成wsdl和用工具反向生成代理程序的规则是什么呢?下来对此进行实例分析:
2.1 先看.NET生成wsdl的规则
实验时所用的开发工具为VS 2010,用vs开发WebService的资料,百度搜索一大推,这里不赘述。
2.1.1、定义数据实体:Person.cs

public class Person {  public int Id { get; set; } //值类型  public string Name { get; set; }//普通引用类型  public int? PhoneNbr { get; set; }//包装类型}

2.1.2、定义服务类:WebService.cs

/// <summary>///WebService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class WebService : System.Web.Services.WebService {  public WebService () {    //如果使用设计的组件,请取消注释以下行     //InitializeComponent();   }  [WebMethod]  public string HelloWorld(int id,int? phoneNbr,string name,Person person) {    return "Hello World";  }  }

helloWorld方法有4个参数,前三个参数和Person类的三个属性是一样的,加入这三个参数的目的是进行对比测试。
2.1.3、查看生成的wsdl

<?

2.1.4、得出结论  
* id参数和Person.Id属性都为值类型:[元素节点] 都必输,[元素取值] 都不能为空
* phoneNbr参数和Person.PhoneNbr属性都是包装类型:[元素节点] 都必输,但[元素取值] 都可以为空;
* name参数和Person.Name属性都是普通引用类型:[元素节点] 都可以不输,但[元素取值] 都不能为空;
* person参数是普通引用类型:[元素节点] 可以不输,但[元素取值] 不能为空。
2.2 再看Java的规则
首先,先熟悉在eclipse中快速开发webservice程序的步骤,参考链接:http://www.blogjava.net/parable-myth/archive/2010/08/03/327802.html,此次实验采用的就是这种快速开发方式(注:不同的框架和工具会可能会有不同的生成规则),所用ecllipse版本为:
    Version: Juno Service Release 1
    Build id: 20121004-1855
2.2.1、定义数据实体:Person.java

package com.lubiao.axis;public class Person implements java.io.Serializable {  private int id;//编号,值类型  private java.lang.String name;//姓名,普通引用类型  private Integer phoneNbr;//电话,包装类型    public int getId() {return id;}  public void setId(int id) {this.id = id;}	  public java.lang.String getName() {return name;}  public void setName(java.lang.String name) {this.name = name;}  public Integer getPhoneNbr() {return phoneNbr;}  public void setPhoneNbr(Integer phoneNbr) {this.phoneNbr = phoneNbr;}}

2.2.2、定义服务类:WebServiceTest.java

package com.lubiao.axis;public class WebServiceTest{	public String helloWorld(int id,Integer phoneNbr,String name,Person person){		return "Hello World";	}}

helloWorld方法有4个参数,前三个参数和Person类的三个属性是一样的,加入这三个参数的目的是进行对比测试。
2.2.3、查看生成的Wsdl

<?

2.2.4、得出结论
* id参数和Person.id属性都是值类型:[元素节点] 都必输,[元素取值] 都不能为空;
* phoneNbr参数和Person.phoneNbr属性都是包装类型:[元素节点] 都必输,但前者[元素取值]不能为空,后者[元素取值]可以为空;
* name参数和Person.name属性都是普通的引用类型:[元素节点] 都必输,但前者[元素取值]不能为空,后者[元素取值]可以为空;
* person参数属于普通引用类型:[元素节点]必输,且[元素取值]不能为空;
*总结:此框架生成wsdl时认为:
        1、作为方法参数时,不管是值类型、包装类型还是普通引用类型,[元素节点]都为必输项,且[元素取值]都不能为空;
        2、作为对象属性时,不管是值类型、包装类型还是普通引用类型,[元素节点]都为必输项,其中值类型的[元素取值]不能为空,但包装类型和普通引用类型的[元素取值]可以为空。
2.3、Java和.NET的异同
其一,对于值类型:
            Java和.NET都认为值类型不论是作为 [方法参数时] 还是 [对象属性时] ,[元素节点]都为必输项,且[元素取值]都不能为空。
             (都合理:这符合值类型的特点,总有一个值)
其二,对于包装类型:
            作为方法参数时:java和.NET都认为[元素节点]是必输的,但java认为[元素取值]不能为空,而.NET认为[元素取值]可以为空;
               (.NET合理,Java不合理。java既要求必输又要求值不能为空,那空对象怎么传入?可以通过生成客户端代理进行验证(这里不再演示),对于phoneNbr参数客户端代码变成了int类型,而服务端可是Integer类型)
            作为对象属性时:java和.NET都认为[元素是节点]必输的,而[元素取值]是可以为空的;
                (都合理。java和.net都要求节点必须出现,然后以nillable=true标识可以传入空值)
其三,对于普通引用类型:
            作为方法参数时:java和.NET都认为[元素取值]是不能为空的,但Java同时认为[元素节点]是必输的,而.NET认为[元素节点]是非必输的;
                (.NET合理,Java不合理。java既要求节点必输还要求值不能为空,那空对象怎么传入?)
            作为对象属性时:java认为[元素节点]是必输的、[元素取值]是可空的;.NET认为[元素节点]是非必输的、[元素取值]是不可空的。
                (都合理。java始终要求必输,但以nillable=true标识可以传入空对象;.NET始终要求元素取值不能为空,但以minOccurs=0来标识可以传入空对象)
总结:
作为参数时,java出现了很多不合理的情况,可能是因为选的这个框架有bug,这里不再关心,主要看作为对象属性时的情况。文章最开始的时候说到minOccurs和nillable会有4种组合方式,在上面的实验中java用到了两种,而.NET用到了3种,分别为:
Java:[ minOccurs=1,nillable=false ] 和 [ minOccurs=1,nillable=true ],前者对应值类型,后者对应引用类型(包装类型和普通引用类型),java压根没用minOccrs=0
.NET:[ minOccurs=1,nillable=false ]、[ minOccurs=1,nillable=true ] 和[ minOccurs=0,nillable=false],前者对应值类型,再者对应包装类型,最后对应普通引用类型。
3、Java和.NET由WSDL自动生成代码的规则
接着上面的实验结果,先来做最简单的测试:1、用java的client对接java的service生成代码;2、用.NET的client对接.NET的Service生成代码,然后分别观察Client端自动生成的Person类和Server段的Person是否完全一致。这里不再演示,只说结论,结论是:生成的Person分别和各自Server端的完全相同。
那如果用[ java对接.NET ] 或者[ 用.NET对接java ],会是什么结果呢?等一等,这么问好像有问题,哪有什么谁对接谁,大家对接的都是wsdl,哪里知道wsdl后面是什么平台!!嗯,那就把minOccurs和nillable的四种组合都列出来,看java和.NET的生成规则分别是什么样的,继续。
3.1 先看.NET的规则
3.1.1 定义WSDL,限于篇幅只列出类型定义部分

<?

id对应的是值类型,name对应的是引用类型
3.1.2 生成数据类

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.

分析:
id1:可空类型、特性:IsNullable=true、生成了对应的id1Specified
id2:值类型、特性:IsNullable=false、生成了对应的id2Specified
id3:可空类型、特性:IsNullable=true
id4:值类型、特性:IsNullable=false
name1:引用类型、特性:IsNullable=true
name2:引用类型、特性:IsNullable=false
name3:引用类型、特性:IsNullable=true
name4:引用类型、特性:IsNullable=false
3.1.3 查看SOAP报文
将可为null的属性都置位null,然后查看SOAP报文是什么样子的
C#代码1:

Person p=new Person();p.id1 = null;p.id2 = 123;p.id3 = null;p.id4 = 456;p.name1 = null;p.name2 = null;p.name3 = null;p.name4 = null;

C#代码2:

Person p=new Person();p.id1 = null;p.id1Specified = true;request.id2 = 123;p.id1Specified = true;p.id3 = null;p.id4 = 456;p.name1 = null;p.name2 = null;p.name3 = null;p.name4 = null;

SOAP报文1:对应C#代码1,只贴出Person部分:

<Person>   <id3 xsi:nil="true" />   <id4>456</id4>   <name1 xsi:nil="true" />   <name3 xsi:nil="true" /></Person>

SOAP报文2:对应C#代码2,只贴出Person部分:

<Person>      <id1 xsi:nil="true" />      <id2>123</id2>      <id3 xsi:nil="true" />      <id4>456</id4>      <name1 xsi:nil="true" />      <name3 xsi:nil="true" /></Person>

3.1.4 得出结论
其一:对于值类型和包装类型

minOccurs="0" 组合nillable="true"时:
会生成包装类型(id1),同时会生成对应的id1Specified属性,为什么会生成带Specified的属性呢?这是因为minOccurs="0"的缘故,minOccurs=0意味着元素节点可以不出现,那到底是出现呢?还是不出现呢?工具自己没了主意,所以生成了一个xxxSpecified属性,该属性默认值为false,只有给它指定true的时候[元素节点]才会出现到soap报文中。
minOccurs="0"组合nillable="false"时:
会生成值类型(id2),同时会生成对应的id2Specified属性(原因同上)
minOccurs="1" 组合nillable="true"时:
会生成包装(id3),不会生成Specified属性(因为元素节点必输)。
minOccurs=“1”组合nillable=“false”时:
会生成值类型(id4),不会生成Specified属性(因为元素节点必输)。
其二:对于普通引用类型
name1、name3:
只要nillable=true不管minOccurs等于什么,[null对象]序列化的时候都以[元素取值]为空方式来体现这是一个空对象。
name2、name4:   
只要nillable=false不管minOccurs等于什么,[null对象]序列化时都以[元素节点]不出现的方式来体现这是一个空对象。(上面实验时,Server端对soap报文进行了schema验证,所以name4传null会报错的)

总结:
* .NET在wsdl_2_C# 和C#_2_wsdl时的思路其实是一致的,1、生成值类型还是包装类型先看nillable属性、然后再看minOccurs属性来控制是否生成xxxSpecified属性;2、而生成普通引用类型时只关心nillable属性,nillable=true就采用nil=true的方式发送null对象,nillabe=false则采用[元素节点]不出现的方式发送null对象,压根就不关心minOccurs是神马。
* 前面说到name4=null时,Server端会报错,所以 对于引用类型:minOccurs=1和nillable=false的组合是没有意义的,这种组合无法让空对象传输过来
3.2 再看Java的规则

3.2.1 定义WSDL,限于篇幅只列出类型定义部分

<?


和.net相比,此处多了一个类型PersonRequest,之所以改成这样是因为:如果按照net那种方式,生成的java代码中不会有Person类,因为工具认为id1,id2....是GetPerson的参数。
3.1.2 生成数据类

/** * Person.java * * This file was auto-generated from WSDL * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter. */package core.bank.csii.com.system;public class Person implements java.io.Serializable {  private java.lang.Long id1;  private java.lang.Long id2;  private java.lang.Long id3;  private long id4;  private java.lang.String name1;  private java.lang.String name2;  private java.lang.String name3;  private java.lang.String name4;  public Person() {  }  public Person(      java.lang.Long id1,      java.lang.Long id2,      java.lang.Long id3,      long id4,      java.lang.String name1,      java.lang.String name2,      java.lang.String name3,      java.lang.String name4) {      this.id1 = id1;      this.id2 = id2;      this.id3 = id3;      this.id4 = id4;      this.name1 = name1;      this.name2 = name2;      this.name3 = name3;      this.name4 = name4;  }  /**   * Gets the id1 value for this Person.   *   * @return id1   */  public java.lang.Long getId1() {    return id1;  }  /**   * Sets the id1 value for this Person.   *   * @param id1   */  public void setId1(java.lang.Long id1) {    this.id1 = id1;  }  /**   * Gets the id2 value for this Person.   *   * @return id2   */  public java.lang.Long getId2() {    return id2;  }  /**   * Sets the id2 value for this Person.   *   * @param id2   */  public void setId2(java.lang.Long id2) {    this.id2 = id2;  }  /**   * Gets the id3 value for this Person.   *   * @return id3   */  public java.lang.Long getId3() {    return id3;  }  /**   * Sets the id3 value for this Person.   *   * @param id3   */  public void setId3(java.lang.Long id3) {    this.id3 = id3;  }  /**   * Gets the id4 value for this Person.   *   * @return id4   */  public long getId4() {    return id4;  }  /**   * Sets the id4 value for this Person.   *   * @param id4   */  public void setId4(long id4) {    this.id4 = id4;  }  /**   * Gets the name1 value for this Person.   *   * @return name1   */  public java.lang.String getName1() {    return name1;  }  /**   * Sets the name1 value for this Person.   *   * @param name1   */  public void setName1(java.lang.String name1) {    this.name1 = name1;  }  /**   * Gets the name2 value for this Person.   *   * @return name2   */  public java.lang.String getName2() {    return name2;  }  /**   * Sets the name2 value for this Person.   *   * @param name2   */  public void setName2(java.lang.String name2) {    this.name2 = name2;  }  /**   * Gets the name3 value for this Person.   *   * @return name3   */  public java.lang.String getName3() {    return name3;  }  /**   * Sets the name3 value for this Person.   *   * @param name3   */  public void setName3(java.lang.String name3) {    this.name3 = name3;  }  /**   * Gets the name4 value for this Person.   *   * @return name4   */  public java.lang.String getName4() {    return name4;  }  /**   * Sets the name4 value for this Person.   *   * @param name4   */  public void setName4(java.lang.String name4) {    this.name4 = name4;  }  private java.lang.Object __equalsCalc = null;  public synchronized boolean equals(java.lang.Object obj) {    if (!(obj instanceof Person)) return false;    Person other = (Person) obj;    if (obj == null) return false;    if (this == obj) return true;    if (__equalsCalc != null) {      return (__equalsCalc == obj);    }    __equalsCalc = obj;    boolean _equals;    _equals = true &&       ((this.id1==null && other.getId1()==null) ||       (this.id1!=null &&       this.id1.equals(other.getId1()))) &&      ((this.id2==null && other.getId2()==null) ||       (this.id2!=null &&       this.id2.equals(other.getId2()))) &&      ((this.id3==null && other.getId3()==null) ||       (this.id3!=null &&       this.id3.equals(other.getId3()))) &&      this.id4 == other.getId4() &&      ((this.name1==null && other.getName1()==null) ||       (this.name1!=null &&       this.name1.equals(other.getName1()))) &&      ((this.name2==null && other.getName2()==null) ||       (this.name2!=null &&       this.name2.equals(other.getName2()))) &&      ((this.name3==null && other.getName3()==null) ||       (this.name3!=null &&       this.name3.equals(other.getName3()))) &&      ((this.name4==null && other.getName4()==null) ||       (this.name4!=null &&       this.name4.equals(other.getName4())));    __equalsCalc = null;    return _equals;  }  private boolean __hashCodeCalc = false;  public synchronized int hashCode() {    if (__hashCodeCalc) {      return 0;    }    __hashCodeCalc = true;    int _hashCode = 1;    if (getId1() != null) {      _hashCode += getId1().hashCode();    }    if (getId2() != null) {      _hashCode += getId2().hashCode();    }    if (getId3() != null) {      _hashCode += getId3().hashCode();    }    _hashCode += new Long(getId4()).hashCode();    if (getName1() != null) {      _hashCode += getName1().hashCode();    }    if (getName2() != null) {      _hashCode += getName2().hashCode();    }    if (getName3() != null) {      _hashCode += getName3().hashCode();    }    if (getName4() != null) {      _hashCode += getName4().hashCode();    }    __hashCodeCalc = false;    return _hashCode;  }  // Type metadata  private static org.apache.axis.description.TypeDesc typeDesc =    new org.apache.axis.description.TypeDesc(Person.class, true);  static {    typeDesc.set

java生成的Person类没有Specified属性,看来它只支持一种形式,下面进行验证
3.2.3查看SOAP报文
java代码:

Person p=new Person();p.setId1(null);p.setId2(null);p.setId3(null);p.setId4(0);p.setName1(null);p.setName2(null);p.setName3(null);p.setName4("123");//.NET实验时,name4传的是null;但如果此处赋null,报文还未发出去,客户端框架就报错:提示不能为null了。

Soap报文:

<?

3.2.4 得出结论:
其一:对于值类型和包装:
  1、除 [ minOccurs=1,nillable=false ] 这一组合除外,生成的都是包装类型。
  2、当对象属性为null时,java根据minOccurs构造soap报文,如果minOccurs=0则[元素节点]不出现代表null,如果minOccurs=1用nil=true表示null。
其二:对于普通引用类型
  生成的肯定是引用类型(这是废话),当对象属性为null是,构造soap报文的规则和上面的2一样
4、深入浅出,最后的总结
不管是由代码生成wsdl,还是由wsdl生成代码,此实验中java都比.NET要简洁,个人比较喜欢java的方式,.NET显得有点儿啰嗦(它把包装类型和普通引用类型做了区分),下面进行最后的总结。
先来总结由代码生成wsdl
java用到了两种组合:
   [minOccurs=1、
nillable=false]:只有值类型用这一组合
   [minOccurs=1、
nillable=true]:包装类型和普通引用类型用这一组合
   注:其实Java用[minOccurs=0 和nillable=false]代表包装类型和普通引用类型也完全可以,如果是我的话我会用这一组合代替上面的组合,这样可以节省网络流量
而.NET有3种组合:
   [ minOccurs=1、nillable=false]:只有值类型用这一组合
   [ minOccurs=1、nillable=true ] :包装类型用这一组合
   [ minOccurs=0、nillable=false]:普通引用类型用这一组合
现在如果要我们自己写一个生成wsdl的框架,那应该有如下的原则:
   1>  值类型只能用
[minOccurs=1、nillable=false],而包装类型和普通引用类型不能用这一组合(否则空对象无法表示) 
   
2>  包装类型可以用[minOccurs=1、nillable=true]和[ minOccurs=0、nillable=false]
  3> 普通引用类型可以用
[minOccurs=1、nillable=true]和[ minOccurs=0、nillable=false]
  
4> 对于包装类型和普通引用类型不建议用[minOccurs=0、nillable=true],这一组合意义不够明确,究竟是空报文节点表示null呢?还是nill=true表示null呢?当然如果真这么用肯定也没问题,只要序列化工具支持就行。

再来看由wsdl生成代码
我是很难理解微软为什么要把[包装类型]和[普通引用类型]区别对待,在C#_2_WSDL的时候这么区别开到还没什么,但是当WSDL_2_C#的时候就带来很大的麻烦。
1、首先来看<element name="long" type="dateTime" minOccurs="0" nillable="false"/>,对这一组合微软生成的是值类型而不是包装类型,它认为只有nillable=true才代表包装类型,可包装类型和普通引用类型本质上有太大的区别吗?大家都是引用类型,[minOccurs=1、nillable=true]和[
minOccurs=0、nillable=false]应该都可以被解析为包装类型才合适。你微软如果认为nillable=true和包装类型(微软的可空类型)联系那么紧密,那这样的节点<element name="name3" type="string" minOccurs="1" nillable="true"/>最好还是别支持了,但现在普通应用类型你既支持minOccurs=0又支持nillable=true,那包装类型和普通引用类型分开的必要性究竟是神马!!!!!!
2、再来看xxxSpecified,前面说到,微软认为对于像type=int这样的节点,如果element定义中出现了minOccurs=0,则要生成一个xxxSpecified属性。这么搞使用起来简直太麻烦了——让程序员指定xxxSepcified为true或false来控制元素节点要不要显示,这种麻烦还是由于“区别对待”带来的。微软只通过nillable判断是否是包装类型,对于minOccurs=0的情况它不得不生成一个xxxSpecified。而如果不区别对待的话就简单多了,只要minOccurs=0和nillable=true随便哪个出现或同时出现,直接生成包装类型。

最后来总结序列化
序列化的时候,java和.NET在处理上也不尽相同:
1、java优先考虑minOccurs,如果minOccurs=0,那不管nillable等于神马,程序中的null以[元素节点]不出现的方式来体现;如果minOccurs=1,则程序中的null以[元素取值]为空的方式来体现;这样的规则同时适用于值类型、包装类型和普通引用类型。
2、而.NET则优先考虑nillable,如果nillable=true,那不管minOccurs等于神马,程序中的null以[元素取值]为空的方式来体现;如果nillable=false,则程序中的null以[元素节点]不出现的方式来体现;这样的规则同时适用于值类型、包装类型和普通引用类型。(注:Specified=true的前提下)

 

 

引用地址: http://www.xuebuyuan.com/1925655.html




原标题:wsdl 关于nillable和minOccurs 在.NET和java中的不同

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

易派国际物流:https://www.goluckyvip.com/tag/47665.html
易派支付 Epay :https://www.goluckyvip.com/tag/47666.html
易票联支付:https://www.goluckyvip.com/tag/47667.html
易评网:https://www.goluckyvip.com/tag/47668.html
易奇云:https://www.goluckyvip.com/tag/47669.html
物流配送时间:https://www.goluckyvip.com/tag/4767.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流