本文介紹.Net 與 Java 相互調用的例子。下面的介紹主要包括三方面:一是通過常用Web服務進行相互調用,二是使用TCP/IP套接字進行相互調用,三是使用Remote實現遠程對象相互調用。
首先說一下Web服務的來源,Web服務是一種新的Web應用程序分支,可以執行從簡單的請求到復雜商務處理等任何功能。一旦部署以后,其他Web服務應用程序可以發現並調用它部署的服務。 Web Service是一種應用程序,它可以使用標准的互聯網協議,像超文件傳輸協議(HTTP)、簡單對象訪問協議(SOAP)、XML等,將功能綱領性地體現在互聯網和企業內部網上,Web服務被視作是Web上的組件編程。Web服務必須提供一套標准的類型系統,用於溝通不同平台、編程語言和組件模型中的不同類型系統。
XML和XSD
可擴展的標記語言XML 是Web Service平台中表示數據的基本格式。除了易於建立和易於分析外,XML主要的優點在於它既與平台無關,又與廠商無關。XML是由萬維網協會 (W3C)創建,W3C制定的XML SchemaXSD 定義了一套標准的數據類型,並給出了一種語言來擴展這套數據類型。 Web Service平台是用XSD來作為數據類型系統的。當你用某種語言如JAVA、C#來構造一個Web Service時,為了符合Web Service標准,所有你使用的數據類型都必須被轉換為XSD類型。如想讓它使用在不同平台和不同軟件的不同組織間傳遞,還需要通過SOAP協議將它包裝起來。
SOAP
SOAP即簡單對象訪問協議(Simple Object Access Protocol),它是用於交換XML編碼信息的輕量級協議。它有三個主要方面:XML-envelope為描述信息內容和如何處理內容定義了框架,將程序對象編碼成為XML對象的規則,執行遠程過程調用(RPC)的約定。SOAP可以運行在任何其他傳輸協議上。例如,你可以使用 SMTP,即因特網電子郵件協議來傳遞SOAP消息,這可是很有誘惑力的。在傳輸層之間的頭是不同的,但XML有效負載保持相同。Web Service 希望實現不同的系統之間能夠用“軟件-軟件對話”的方式相互調用,打破了軟件應用、網站和各種設備之間的格格不入的狀態,實現“基於Web無縫集成”的目標。
WSDL
Web Service描述語言WSDL 就是用機器能閱讀的方式提供的一個正式描述文檔而基於XML的語言,用於描述Web Service及其函數、參數和返回值。因為是基於XML的,所以WSDL既是機器可閱讀的,又是人可閱讀的。
下面分別以實例說明如何通過Web服務實現JAVA與.NET的相互調用。
一、使用JAVA作為服務器端,.NET作為客戶端,實現 .Net 調用 Java 接口。
JAVA開發Web Service的工具有很多,最常用的有Axis、XFire、NetBean等,在JAVA-SE 6.0以上支持JAX-WS2.0 ,JAX-WS 2.0是JAX-RPC 1.0的更新產品。在 JAX-WS中,一個遠程調用可以轉換為一個基於XML的協議例如SOAP。在使用JAX-WS過程中,開發者不需要編寫任何生成和處理SOAP消息的代碼。JAX-WS的運行時實現會將這些API的調用轉換成為對於SOAP消息。 在服務器端,用戶只需要通過Java語言定義遠程調用所需要實現的接口SEI (service endpoint interface),並提供相關的實現,通過調用JAX-WS的服務發布接口就可以將其發布為WebService接口。在下面我們就以XFire建立一個Web Service。
首先建立一個在一個項目上單擊右鍵,選擇MyEclipse->Add XFire Web Service Capabilities,引用了XFire工具包以后。在項目會自動建立一個WebServices文件夾,文件夾里面的service.xml就是對發布Web Service進行配置的。
首先,建立好一個服務層

//建立一個Model包,里面包含一個值對象Person package Model; import java.io.Serializable; publicclass Person implements Serializable { privateint id; private String name; privateint age; publicint getId(){ return id; } publicvoid setId(int id){ this.id=id; } public String getName(){ return name; } publicvoid setName(String name){ this.name=name; } publicint getAge(){ return age; } publicvoid setAge(int age){ this.age=age; } } //建立一個Service包,里面包含服務接口 package Service; import java.util.List; import Model.*; publicinterface PersonService { List<Person> GetList(); } //建立一個ServiceImpl包,實現服務 package ServiceImpl; import Model.*; import Service.*; import java.util.*; publicclass PersonServiceImpl implements PersonService{ public List<Person> GetList(){ List<Person> personList=new LinkedList<Person>(); Person person1=new Person(); person1.setId(0); person1.setAge(23); person1.setName("Leslie"); personList.add(person1); Person person2=new Person(); person2.setId(1); person2.setAge(30); person2.setName("Mike"); personList.add(person2); return personList; } }
然后,在Service.xml上面對服務進行配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>PersonService</name> <namespace>http://leslie-pc:8080/PersonService</namespace> <serviceClass> Service.PersonService </serviceClass> <implementationClass> ServiceImpl.PersonServiceImpl </implementationClass> </service> </beans>
配置功能如下:
- service:service 標簽和它所包含的 xml 內容為發布成 Web 服務的 POJO 提供完整的描述。
- name:Web 服務被發布時所采用的唯一名稱。
- namespace:Web 服務發布時所使用的命名空間。
- serviceClass:Web 服務接口類的全名,包括包名和類名。
- implemetationClass:Web 服務實現類的全名,包括包名和類名。
現在可以運行程序,對服務進行測試,在測試時輸入服務地址http://leslie-pc:8080/WebSite1/services/PersonService?wsdl,系統將顯示wsdl代碼

<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions targetNamespace="http://leslie-pc:8080/PersonService" xmlns:ns1="http://Model" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://leslie-pc:8080/PersonService" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> - <wsdl:types> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://leslie-pc:8080/PersonService"> - <xsd:element name="GetList"> <xsd:complexType /> </xsd:element> - <xsd:element name="GetListResponse"> - <xsd:complexType> - <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns1:ArrayOfPerson"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://Model"> - <xsd:complexType name="ArrayOfPerson"> - <xsd:sequence> <xsd:element maxOccurs="unbounded" minOccurs="0" name="Person" nillable="true" type="ns1:Person"/> </xsd:sequence> </xsd:complexType> - <xsd:complexType name="Person"> - <xsd:sequence> <xsd:element minOccurs="0" name="age" type="xsd:int"/> <xsd:element minOccurs="0" name="id" type="xsd:int"/> <xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> - <wsdl:message name="GetListRequest"> <wsdl:part name="parameters" element="tns:GetList"/> </wsdl:message> - <wsdl:message name="GetListResponse"> <wsdl:part name="parameters" element="tns:GetListResponse"/> </wsdl:message> - <wsdl:portType name="PersonServicePortType"> - <wsdl:operation name="GetList"> <wsdl:input name="GetListRequest" message="tns:GetListRequest"/> <wsdl:output name="GetListResponse" message="tns:GetListResponse"/> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="PersonServiceHttpBinding" type="tns:PersonServicePortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> - <wsdl:operation name="GetList"> <wsdlsoap:operation soapAction=""/> - <wsdl:input name="GetListRequest"> <wsdlsoap:body use="literal"/> </wsdl:input> - <wsdl:output name="GetListResponse"> <wsdlsoap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="PersonService"> - <wsdl:port name="PersonServiceHttpPort" binding="tns:PersonServiceHttpBinding"> <wsdlsoap:address location="http://leslie-pc:8080/WebSite1/services/PersonService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
服務器端測試已經成功,現在使用.NET對服務進行調用,在項目上單擊右鍵->選擇添加Web服務->在URL地址上輸入服務的地址http://leslie-pc:8080/WebSite1/services/PersonService?wsdl ,在一個頁面上輸入代碼進行測試。
protectedvoid Page_Load(object sender, EventArgs e) { Service.PersonService personService =new Service.PersonService(); IList<Service.Person> personList = personService.GetList(); foreach(Service.Person person in personList) { Response.Write("id:"+ person.id.ToString() +" name:"+ person.name +" age:"+ person.age.ToString()+"<br/>"); } }
測試成功的話,恭喜你,你已經了解到JAVA與.NET是如何通過Web服務進行相互調用的了。但因為Web服務從本質是就是不受開發語言的局限的,所以只要閣下對JAVA跟.NET有一定了解,要通過Web服務實現相互調用相信不是一個難題。但往往在一些ERP,OA的開發過程,會在很多時候使用TCP/IP套接字實現軟件的功能,TCP/IP這“老家伙”為何使用了這么長時間還會經常見到它的身影,這是因為使用TCP/IP有着更高效率,而且易於通過防火牆的阻隔,而HTTP協議也是建立一TCP/IP之上的。在下一章將為大家介紹JAVA與.NET是如何通過TCP/IP套接字進行相互調用的。
二、使用.NET作為服務器端,JAVA作為客戶端實現相互調用。
在.NET系統里面,以WCF作為新一代的服務開發工具是微軟的一個新賣點,我們就以WCF為例子實現服務器端,首先新建一個網站項目,在網站添加一個WCF服務PersonService。你將看到PersonService.svc、IPersonService、PersonService.cs三個文件,其中IPersonService是對向暴露一個接口,接口的功能由PersonService來實現,客戶端則通過PersonalService.svc來尋獲服務,並對其添加引用的。
1、在PersonService.svc里,只包括一行,其中列明了該服務的實現
<%@ ServiceHost Language="C#" Debug="true" Service="Web.PersonService" CodeBehind="PersonService.svc.cs" %>
[1]Service:命名空間
[2]CodeBehind:實現服務的后台代碼類
2、實現服務的后台代碼類PersonService.svc.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace Web { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IPersonService”。 [ServiceContract] public interface IPersonService { [OperationContract] IList<Person> GetList(); } // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼、svc 和配置文件中的類名“PersonService”。 public class PersonService : IPersonService { public IList<Person> GetList() { IList<Person> personList = new List<Person>(); Person person1 = new Person(); person1.ID = 0; person1.Age = 27; person1.Name = "Leslie"; personList.Add(person1); Person person2 = new Person(); person2.ID = 1; person2.Age = 23; person2.Name = "Rose"; personList.Add(person2); Person person3 = new Person(); person3.ID = 2; person3.Age = 29; person3.Name = "Jack"; personList.Add(person3); return personList; } } }
為了使用Person能夠實現遠程傳送,我們必須對Person進行序列化,在WCF中包括服務契約、數據契約、消息契約三部分,而數據契約正是用於對數據進行序列化處理的。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.Serialization; ///<summary> ///Person 的摘要說明 ///</summary> namespace Web { [DataContract] public class Person { [DataMember] public int ID{ get; set; } [DataMember] public string Name { get; set; } [DataMember] public int Age { get; set; } } }
數據契約里面有多種的序列化方式,包括DataContractSerializer,NetDataContractSerializer,XmlServializer,DataContractJsonSerializer。在這里面只用使用最普遍的DataContractSerializer,而DataContractJsonSerializer是現今比較熱門的方式,特別是在開發網絡項目時候,多使用Json進行數據通訊。
最后配置好web.config,就可以成功將WCF服務發布
<?xml version="1.0"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="serviceBehavior"> <!--注意將httpGetEnabled設置為true,使客戶端能夠成功捕獲服務--> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <services> <!--name屬性必須與服務實現類的類名相對應--> <service name="Service.PersonService" behaviorConfiguration="serviceBehavior"> <!--contract必須與契約名相對應--> <endpoint address="" binding="basicHttpBinding" contract="Service.IPersonService"/> <!--注意打開元數據,使客戶能下載--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> </system.serviceModel> </configuration>
下面使用MyEclipse8.6進行客戶端開發,首先添加對服務的引用,按Ctrl+N新建一個項目,選擇Web Service->Web Service Client,單擊下一步,這時候選擇在Framework上選擇JAX-WS,單擊下一步
在WSDL URL上輸入服務的路徑,並為服務添加一個Java pagckage包myServices,點擊完成,這樣WCF服務便可成功加入到客戶端。
此時為此項目添加測試類,運行進行測試
package myAssembly; import java.util.List; publicclass Test { publicstaticvoid main(String[] args){ myServices.PersonService service=new myServices.PersonService(); //獲取服務對象 myServices.IPersonService personService=service.getBasicHttpBindingIPersonService(); //通過basicHttpBinding協議綁定遠程對象 List<myServices.Person> personList=personService.getList().getPerson(); for(int n=0;n<personList.size();n++){ System.out.println("ID:"+personList.get(n).getID()+" Name:"+personList.get(n).getName().toString()+" Age:"+personList.get(n).getAge()); } } }