Interop .NET and J2EE via WebService Contract-first approach

VisualStudio引進的ASMX的確大大簡化了XML Web Services的開發過程,但也存在一個比較突出的弊端,那便是VisualStudio的設計使用的是Code-first approach,先編寫代碼,再由.NET Framework動態生成Web Services的WSDL,而不是實施SOA更加認同的Contract-first approach。近日實踐了一把在開發ASMX Web Services時運用Contract-first approach,然後通過bea Weblogic Workshop來comsume。

Step 1:生成XSD,即Web Method傳入和傳出XML的Schema,比如我有個叫Test1()的Web Method, 那分別爲它定義Test1Request和Test1Response,如下是一個例子:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestInterface" targetNamespace="http://groupm.com/TestInterface.xsd" elementFormDefault="qualified" 

xmlns
="http://groupm.com/TestInterface.xsd" xmlns:mstns="http://groupm.com/TestInterface.xsd" 

xmlns:xs
="http://www.w3.org/2001/XMLSchema">
    
<xs:complexType name="Test1Request">
        
<xs:sequence minOccurs="1" maxOccurs="1">
            
<xs:element name="keyword1" type="xs:string" />
            
<xs:element name="keyword2" type="xs:int" />
        
</xs:sequence>
    
</xs:complexType>
    
<xs:complexType name="Test1Response">
        
<xs:sequence>
            
<xs:element name="records" type="Record" maxOccurs="unbounded" minOccurs="0" />
        
</xs:sequence>
    
</xs:complexType>
    
<xs:element name="Test1Request" type="Test1Request">
    
</xs:element>
    
<xs:element name="Test1Response" type="Test1Response">
    
</xs:element>
    
<xs:complexType name="Record">
        
<xs:sequence>
            
<xs:element name="id" type="xs:int" />
            
<xs:element name="name" type="xs:string" />
        
</xs:sequence>
    
</xs:complexType>
</xs:schema>


Step 2:生成此XSD對應的.NET類,命令爲:xsd.exe /classes filename.xsd,有了這個類文件就可以在Web Method中方便地使用符合Schema的XML數據了(而不是手工去分析傳入的XML數據)。

Step 3:生成WSDL,我用了能集成進VisualStudio的WSCF工具來完成這個工作,其間設定了Web Method及其參數和返回值使用的Schema,以下是一個例子:

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://groupm/TestService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:import1="http://tempuri.org/XMLSchema1.xsd" targetNamespace="http://groupm/TestService" name="TestService" xmlns="http://schemas.xmlsoap.org/wsdl/">
  
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
  
<types>
    
<xsd:schema>
      
<xsd:import schemaLocation="D:\Daniel\My Documents\Visual Studio 2005\Projects\M21116-2\M2WebService\XMLSchema1.xsd" namespace="http://tempuri.org/XMLSchema1.xsd" />
    
</xsd:schema>
  
</types>
  
<message name="testIn">
    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
    
<part name="messagePart" element="import1:TestRequest" />
  
</message>
  
<message name="testOut">
    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
    
<part name="messagePart" element="import1:TestResponse" />
  
</message>
  
<portType name="TestServiceInterface">
    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
    
<operation name="Test">
      
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
      
<input message="tns:testIn" />
      
<output message="tns:testOut" />
    
</operation>
  
</portType>
  
<binding name="TestService" type="tns:TestServiceInterface">
    
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    
<operation name="Test">
      
<soap:operation soapAction="http://groupm/TestService:Test" style="document" />
      
<input>
        
<soap:body use="literal" />
      
</input>
      
<output>
        
<soap:body use="literal" />
      
</output>
    
</operation>
  
</binding>
  
<service name="WebService">
    
<port name="WebServiceSoap" binding="tns:WebServiceSoap">
      
<soap:address location="http://localhost/M2WebService/WebService.asmx" />
    
</port>
  
</service>
</definitions>

注意:最後的service標籤,工具不會自動生成,需要手工加入。

Step 4:生成.NET代碼,這一步我也使用WSCF來完成,生成的是一個Interface和一個ASMX,此ASMX已經可以通過瀏覽器查看,只不過還未實現其中的業務邏輯。

Step 5:編寫ASMX的業務邏輯代碼,使Web Service能夠真正運作。
注意:每次使用WSCF都會重新生成接口和ASMX,所以如果WSDL發生變化而需要更新,須注意不要覆蓋了原先的代碼。

Step 6:在Weblogic Workshop中新建一個Application,在Schema中import Step 1中生成的XSD文件,此時Weblogic會編譯此文件並生成相應的XML Bean。

Step 7:在默認的Project中新建一個Java Control,選擇Web Service,設定WSDL爲Step 2中生成的文件,完成後便可通過這個控件開始使用此Web Service。
注意:Weblogic提供了完備的測試機制,可以自動生成測試文件,並可在測試過程中通過XML來傳入Web Method的參數,這點比VisualStudio方便很多。

總結:目前此種開發方式雖然達到了很高的互操作能力,但由於集成開發環境的缺憾,生產效率會被大大降低,所以是否採用Contract-first approach還是的根據業務的需求來確定。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章