Interop .NET and J2EE via WebService Contract-first approach

本文介绍了在开发ASMX Web Services时运用Contract-first approach的实践过程,包括生成XSD、.NET类、WSDL、.NET代码,编写业务逻辑,以及在Weblogic Workshop中使用Web Service等步骤。指出该开发方式互操作性高,但因集成环境缺憾会降低生产效率,是否采用需根据业务需求确定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,如下是一个例子:

None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
<xs:schema id="TestInterface" targetNamespace="http://groupm.com/TestInterface.xsd" elementFormDefault="qualified" 
None.gif
None.gifxmlns
="http://groupm.com/TestInterface.xsd" xmlns:mstns="http://groupm.com/TestInterface.xsd" 
None.gif
None.gifxmlns:xs
="http://www.w3.org/2001/XMLSchema">
None.gif    
<xs:complexType name="Test1Request">
None.gif        
<xs:sequence minOccurs="1" maxOccurs="1">
None.gif            
<xs:element name="keyword1" type="xs:string" />
None.gif            
<xs:element name="keyword2" type="xs:int" />
None.gif        
</xs:sequence>
None.gif    
</xs:complexType>
None.gif    
<xs:complexType name="Test1Response">
None.gif        
<xs:sequence>
None.gif            
<xs:element name="records" type="Record" maxOccurs="unbounded" minOccurs="0" />
None.gif        
</xs:sequence>
None.gif    
</xs:complexType>
None.gif    
<xs:element name="Test1Request" type="Test1Request">
None.gif    
</xs:element>
None.gif    
<xs:element name="Test1Response" type="Test1Response">
None.gif    
</xs:element>
None.gif    
<xs:complexType name="Record">
None.gif        
<xs:sequence>
None.gif            
<xs:element name="id" type="xs:int" />
None.gif            
<xs:element name="name" type="xs:string" />
None.gif        
</xs:sequence>
None.gif    
</xs:complexType>
None.gif
</xs:schema>


Step 2:生成此XSD对应的.NET类,命令为:xsd.exe /classes filename.xsd,有了这个类文件就可以在Web Method中方便地使用符合Schema的XML数据了(而不是手工去分析传入的XML数据)。

Step 3:生成WSDL,我用了能集成进VisualStudio的WSCF工具来完成这个工作,其间设定了Web Method及其参数和返回值使用的Schema,以下是一个例子:

None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
<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/">
None.gif  
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
None.gif  
<types>
None.gif    
<xsd:schema>
None.gif      
<xsd:import schemaLocation="D:\Daniel\My Documents\Visual Studio 2005\Projects\M21116-2\M2WebService\XMLSchema1.xsd" namespace="http://tempuri.org/XMLSchema1.xsd" />
None.gif    
</xsd:schema>
None.gif  
</types>
None.gif  
<message name="testIn">
None.gif    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
None.gif    
<part name="messagePart" element="import1:TestRequest" />
None.gif  
</message>
None.gif  
<message name="testOut">
None.gif    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
None.gif    
<part name="messagePart" element="import1:TestResponse" />
None.gif  
</message>
None.gif  
<portType name="TestServiceInterface">
None.gif    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
None.gif    
<operation name="Test">
None.gif      
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
None.gif      
<input message="tns:testIn" />
None.gif      
<output message="tns:testOut" />
None.gif    
</operation>
None.gif  
</portType>
None.gif  
<binding name="TestService" type="tns:TestServiceInterface">
None.gif    
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
None.gif    
<operation name="Test">
None.gif      
<soap:operation soapAction="http://groupm/TestService:Test" style="document" />
None.gif      
<input>
None.gif        
<soap:body use="literal" />
None.gif      
</input>
None.gif      
<output>
None.gif        
<soap:body use="literal" />
None.gif      
</output>
None.gif    
</operation>
None.gif  
</binding>
None.gif  
<service name="WebService">
None.gif    
<port name="WebServiceSoap" binding="tns:WebServiceSoap">
None.gif      
<soap:address location="http://localhost/M2WebService/WebService.asmx" />
None.gif    
</port>
None.gif  
</service>
None.gif
</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还是的根据业务的需求来确定。


转载于:https://www.cnblogs.com/daniel_ngn/archive/2005/11/18/279622.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值