天天看點

JAVA與.NET的互相調用——通過Web服務實作互相調用

JAVA與.NET是現今世界競争激烈的兩大開發媒體,兩者語言有很多相似的地方。而在很多大型的開發項目裡面,往往需要使用兩種語言進行內建開 發。而很多的開發人員都會偏向于其中一種語言,在使用內建開發的時候對另一種語言感覺到畏懼。在這裡在下向各位介紹一下,JAVA與.NET互相調用的例 子。下面的介紹主要包括三方面:一是通過常用Web服務進行互相調用,二是使用TCP/IP套接字進行互相調用,三是使用Remote實作遠端對象互相調 用。

在這章裡面先為大家介紹一下最簡單,最常用的Web服務互相調用方式。首先說一下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的互相調用。

一、使用.NET作為伺服器端,JAVA作為用戶端實作互相調用。

在.NET系統裡面,以WCF作為新一代的服務開發工具是微軟的一個新賣點,我們就以WCF為例子實作伺服器端,首先建立一個網站項目,在網站添加 一個WCF服務PersonService。你将看到PersonService.svc、IPersonService、 PersonService.cs三個檔案,其中IPersonService是對向暴露一個接口,接口的功能由PersonService來實作,客戶 端則通過PersonalService.svc來尋獲服務,并對其添加引用的。

  1. //在PersonService.svc裡,隻包括一行,其中列明了該服務的實作類 
  2. <%@ ServiceHost Language="C#" Debug="true" Service="Service.PersonService"
  3. CodeBehind="~/App_Code/PersonService.cs" %> 
  4. // 注意: 使用“重構”菜單上的“重命名”指令,可以同時更改代碼和
  5. // 配置檔案中的接口名“IPersonService”。 
  6. namespace Service 
  7.     [ServiceContract] 
  8.     public interface IPersonService 
  9.     { 
  10.         [OperationContract] 
  11.         IList<Person> GetList(); 
  12.     } 
  13.     public class PersonService : IPersonService 
  14.     { 
  15.         public IList<Person> GetList() 
  16.         { 
  17.             IList<Person> personList = new List<Person>(); 
  18.             Person person1 = new Person(); 
  19.             person1.ID = 0; 
  20.             person1.Age = 27; 
  21.             person1.Name = "Leslie"; 
  22.             personList.Add(person1); 
  23.             Person person2 = new Person(); 
  24.             person2.ID = 1; 
  25.             person2.Age = 23; 
  26.             person2.Name = "Rose"; 
  27.             personList.Add(person2); 
  28.             Person person3 = new Person(); 
  29.             person3.ID = 2; 
  30.             person3.Age = 29; 
  31.             person3.Name = "Jack"; 
  32.             personList.Add(person3); 
  33.             return personList; 
  34.         } 
  35.     } 

為了使用Person能夠實作遠端傳送,我們必須對Person進行序列化,在WCF中包括服務契約、資料契約、消息契約三部分,而資料契約正是用于對資料進行序列化處理的,如果想對WCF有進一步的了解,可以連結使用WCF實作SOA面向服務程式設計

  1. namespace Service 
  2.     //建立Person資料契約 
  3.     [DataContract] 
  4.     public class Person 
  5.     { 
  6.         [DataMember] 
  7.         public int ID 
  8.         { 
  9.             get; 
  10.             set; 
  11.         } 
  12.         [DataMember] 
  13.         public string Name 
  14.         { 
  15.             get; 
  16.             set; 
  17.         } 
  18.         [DataMember] 
  19.         public int Age 
  20.         { 
  21.             get; 
  22.             set; 
  23.         } 
  24.     } 

資料契約裡面有多種的序列化方式,包括 DataContractSerializer,NetDataContractSerializer,XmlServializer,DataContractJsonSerializer。 在這裡面隻用使用最普遍的DataContractSerializer,而DataContractJsonSerializer是現今比較熱門的方 式,特别是在開發網絡項目時候,多使用Json進行資料通訊。

最後配置好web.config,就可以成功将WCF服務釋出

  1. <?xml version="1.0"?> 
  2. <configuration> 
  3.     <system.web> 
  4.         <compilation debug="true" targetFramework="4.0"> 
  5.             <assemblies> 
  6.                 <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, 
  7.              PublicKeyToken=B77A5C561934E089"/> 
  8.       </assemblies></compilation> 
  9.     </system.web> 
  10.     <system.serviceModel> 
  11.         <behaviors> 
  12.             <serviceBehaviors> 
  13.                 <behavior name="serviceBehavior"> 
  14.                     <!--注意将httpGetEnabled設定為true,使用戶端能夠成功捕獲服務--> 
  15.                     <serviceMetadata httpGetEnabled="true"/> 
  16.                     <serviceDebug includeExceptionDetailInFaults="false"/> 
  17.                 </behavior> 
  18.             </serviceBehaviors> 
  19.         </behaviors> 
  20.     <services> 
  21.        <!--name屬性必須與服務實作類的類名相對應--> 
  22.       <service name="Service.PersonService" behaviorConfiguration="serviceBehavior"> 
  23.         <!--contract必須與契約名相對應--> 
  24.         <endpoint address="" binding="basicHttpBinding" contract="Service.IPersonService"/> 
  25.          <!--注意打開中繼資料,使客戶能下載下傳--> 
  26.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
  27.       </service> 
  28.     </services> 
  29.     </system.serviceModel> 
  30. </configuration> 

下面使用MyEclipse8.6進行用戶端開發,首先添加對服務的引用,按Ctrl+N建立一個項目,選擇Web Service->Web Service Client,單擊下一步,這時候選擇在Framework上選擇JAX-WS,單擊下一步

JAVA與.NET的互相調用——通過Web服務實作互相調用

在WSDL URL上輸入服務的路徑,并為服務添加一個Java pagckage包myServices,點選完成,這樣WCF服務便可成功加入到用戶端。

此時為此項目添加測試類,運作進行測試

  1. package myAssembly; 
  2. import java.util.List; 
  3. public class Test { 
  4.    public static void main(String[] args){ 
  5.        myServices.PersonService service=new myServices.PersonService(); 
  6.        //擷取服務對象 
  7.        myServices.IPersonService personService= 
  8.            service.getBasicHttpBindingIPersonService(); 
  9.        //通過basicHttpBinding協定綁定遠端對象 
  10.        List<myServices.Person> personList=personService.getList().getPerson(); 
  11.        for(int n=0;n<personList.size();n++){ 
  12.            System.out.println("ID:"+personList.get(n).getID()+ 
  13.                    " Name:"+personList.get(n).getName().toString()+ 
  14.                    " Age:"+personList.get(n).getAge()); 
  15.        } 
  16.    } 

二、使用JAVA作為伺服器端,.NET作為用戶端實作互相調用。

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進行配置的。

現在先建立好一個服務層

  1. //建立一個Model包,裡面包含一個值對象Person 
  2. package Model; 
  3. import java.io.Serializable; 
  4. public class Person implements Serializable { 
  5.     private int id; 
  6.     private String name; 
  7.     private int age; 
  8.     public int getId(){ 
  9.         return id; 
  10.     } 
  11.     public void setId(int id){ 
  12.         this.id=id; 
  13.     } 
  14.     public String getName(){ 
  15.         return name; 
  16.     } 
  17.     public void setName(String name){ 
  18.         this.name=name; 
  19.     } 
  20.     public int getAge(){ 
  21.         return age; 
  22.     } 
  23.     public void setAge(int age){ 
  24.         this.age=age; 
  25.     } 
  26. //建立一個Service包,裡面包含服務接口 
  27. package Service; 
  28. import java.util.List; 
  29. import Model.*; 
  30. public interface PersonService { 
  31.    List<Person> GetList(); 
  32. //建立一個ServiceImpl包,實作服務 
  33. package ServiceImpl; 
  34. import Model.*; 
  35. import Service.*; 
  36. import java.util.*; 
  37. public class PersonServiceImpl implements PersonService{ 
  38.    public List<Person> GetList(){ 
  39.        List<Person> personList=new LinkedList<Person>(); 
  40.        Person person1=new Person(); 
  41.        person1.setId(0); 
  42.        person1.setAge(23); 
  43.        person1.setName("Leslie"); 
  44.        personList.add(person1); 
  45.        Person person2=new Person(); 
  46.        person2.setId(1); 
  47.        person2.setAge(30); 
  48.        person2.setName("Mike"); 
  49.        personList.add(person2); 
  50.        return personList; 
  51.    } 

在Service.xml上面對服務進行配置

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://xfire.codehaus.org/config/1.0"> 
  3. <service> 
  4.     <name>PersonService</name> 
  5.     <namespace>http://leslie-pc:8080/PersonService</namespace> 
  6.     <serviceClass> 
  7.         Service.PersonService 
  8.     </serviceClass> 
  9.     <implementationClass> 
  10.         ServiceImpl.PersonServiceImpl 
  11.     </implementationClass> 
  12. </service> 
  13. </beans> 

其配置功能如下:

  • service

    service 标簽和它所包含的 xml 内容為釋出成 Web 服務的 POJO 提供完整的描述。

  • name

    Web 服務被釋出時所采用的唯一名稱。

  • namespace

    Web 服務釋出時所使用的命名空間。

  • serviceClass

    Web 服務接口類的全名,包括包名和類名。

  • implemetationClass

    Web 服務實作類的全名,包括包名和類名。

現在可以運作程式,對服務進行測試,在測試時輸入服務位址http://leslie-pc:8080/WebSite1/services/PersonService?wsdl,系統将顯示wsdl代碼

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. - <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"> 
  3. - <wsdl:types> 
  4. - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://leslie-pc:8080/PersonService"> 
  5. - <xsd:element name="GetList"> 
  6. <xsd:complexType /> 
  7. </xsd:element> 
  8. - <xsd:element name="GetListResponse"> 
  9. - <xsd:complexType> 
  10. - <xsd:sequence> 
  11. <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns1:ArrayOfPerson" /> 
  12. </xsd:sequence> 
  13. </xsd:complexType> 
  14. </xsd:element> 
  15. </xsd:schema> 
  16. - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://Model"> 
  17. - <xsd:complexType name="ArrayOfPerson"> 
  18. - <xsd:sequence> 
  19. <xsd:element maxOccurs="unbounded" minOccurs="0" name="Person" nillable="true" type="ns1:Person" /> 
  20. </xsd:sequence> 
  21. </xsd:complexType> 
  22. - <xsd:complexType name="Person"> 
  23. - <xsd:sequence> 
  24. <xsd:element minOccurs="0" name="age" type="xsd:int" /> 
  25. <xsd:element minOccurs="0" name="id" type="xsd:int" /> 
  26. <xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string" /> 
  27. </xsd:sequence> 
  28. </xsd:complexType> 
  29. </xsd:schema> 
  30. </wsdl:types> 
  31. - <wsdl:message name="GetListRequest"> 
  32. <wsdl:part name="parameters" element="tns:GetList" /> 
  33. </wsdl:message> 
  34. - <wsdl:message name="GetListResponse"> 
  35. <wsdl:part name="parameters" element="tns:GetListResponse" /> 
  36. </wsdl:message> 
  37. - <wsdl:portType name="PersonServicePortType"> 
  38. - <wsdl:operation name="GetList"> 
  39. <wsdl:input name="GetListRequest" message="tns:GetListRequest" /> 
  40. <wsdl:output name="GetListResponse" message="tns:GetListResponse" /> 
  41. </wsdl:operation> 
  42. </wsdl:portType> 
  43. - <wsdl:binding name="PersonServiceHttpBinding" type="tns:PersonServicePortType"> 
  44. <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
  45. - <wsdl:operation name="GetList"> 
  46. <wsdlsoap:operation soapAction="" /> 
  47. - <wsdl:input name="GetListRequest"> 
  48. <wsdlsoap:body use="literal" /> 
  49. </wsdl:input> 
  50. - <wsdl:output name="GetListResponse"> 
  51. <wsdlsoap:body use="literal" /> 
  52. </wsdl:output> 
  53. </wsdl:operation> 
  54. </wsdl:binding> 
  55. - <wsdl:service name="PersonService"> 
  56. - <wsdl:port name="PersonServiceHttpPort" binding="tns:PersonServiceHttpBinding"> 
  57. <wsdlsoap:address location="http://leslie-pc:8080/WebSite1/services/PersonService" /> 
  58. </wsdl:port> 
  59. </wsdl:service> 
  60. </wsdl:definitions> 

伺服器端測試已經成功,現在使用.NET對服務進行調用,在項目上單擊右鍵->選擇添加Web服務->在URL位址上輸入服務的位址 http://leslie-pc:8080/WebSite1/services/PersonService?wsdl  ,在一個頁面上輸入代碼進行測試。

  1. protected void Page_Load(object sender, EventArgs e) 
  2.     Service.PersonService personService = new Service.PersonService(); 
  3.     IList<Service.Person> personList = personService.GetList(); 
  4.     foreach(Service.Person person in personList) 
  5.     { 
  6.           Response.Write("id:" + person.id.ToString() + " name:" +  
  7.            person.name + " age:" + person.age.ToString()+"<br/>"); 
  8.     } 

測試成功的話,恭喜你,你已經了解到JAVA與.NET是如何通過Web服務進行互相調用的了。但因為Web服務從本質是就是不受開發語言的局限 的,是以隻要閣下對JAVA跟.NET有一定了解,要通過Web服務實作互相調用相信不是一個難題。但往往在一些ERP,OA的開發過程,會在很多時候使 用TCP/IP套接字實作軟體的功能,TCP/IP這“老家夥”為何使用了這麼長時間還會經常見到它的身影,這是因為使用TCP/IP有着更高效率,而且 易于通過防火牆的阻隔,而HTTP協定也是建立一TCP/IP之上的。在下一章将為大家介紹JAVA與.NET是如何通過TCP/IP套接字進行互相調用 的。

原代碼: (由于上傳空間有限,未能将JAVA項目的.metadata一并上傳,請運作時先建立JAVA Project項目,再加入原代碼即可以成功運作)

JAVA伺服器、.NET用戶端

.NET伺服器、.JAVA用戶端

JAVA與.NET的互相調用——利用JNBridge橋接模式實作遠端通訊

cnblogs部落格園:http://www.cnblogs.com/leslies2/

csdn部落格:http://blog.csdn.net/leslies2

繼續閱讀