天天看點

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 是Web Service平台中表示資料的基本格式。除了易于建立和易于分析外,XML主要的優點在于它既與平台無關,又與廠商無關。XML是由網際網路協會 (W3C)建立,W3C制定的XML SchemaXSD 定義了一套标準的資料類型,并給出了一種語言來擴充這套資料類型。 Web Service平台是用XSD來作為資料類型系統的。當你用某種語言如JAVA、C#來構造一個Web Service時,為了符合Web Service标準,所有你使用的資料類型都必須被轉換為XSD類型。如想讓它使用在不同平台和不同軟體的不同組織間傳遞,還需要通過SOAP協定将它包 裝起來。

SOAP即簡單對象通路協定(Simple Object Access Protocol),它是用于交換XML編碼資訊的輕量級協定。它有三個主要方面:XML-envelope為描述資訊内容和如何處理内容定義了架構,将 程式對象編碼成為XML對象的規則,執行遠端過程調用(RPC)的約定。SOAP可以運作在任何其他傳輸協定上。例如,你可以使用 SMTP,即網際網路電子郵件協定來傳遞SOAP消息,這可是很有誘惑力的。在傳輸層之間的頭是不同的,但XML有效負載保持相同。Web Service 希望實作不同的系統之間能夠用“軟體-軟體對話”的方式互相調用,打破了軟體應用、網站和各種裝置之間的格格不入的狀态,實作“基于Web無縫內建”的目 标。

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來尋獲服務,并對其添加引用的。

//在PersonService.svc裡,隻包括一行,其中列明了該服務的實作類 

<%@ ServiceHost Language="C#" Debug="true" Service="Service.PersonService"

CodeBehind="~/App_Code/PersonService.cs" %> 

// 注意: 使用“重構”菜單上的“重命名”指令,可以同時更改代碼和

// 配置檔案中的接口名“IPersonService”。 

namespace Service 

    [ServiceContract] 

    public interface IPersonService 

    { 

        [OperationContract] 

        IList<Person> GetList(); 

    } 

    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資料契約 

    [DataContract] 

    public class Person 

        [DataMember] 

        public int ID 

            get; 

            set; 

        public string Name 

        public int Age 

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

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

<?xml version="1.0"?> 

<configuration> 

    <system.web> 

        <compilation debug="true" targetFramework="4.0"> 

            <assemblies> 

                <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, 

             PublicKeyToken=B77A5C561934E089"/> 

      </assemblies></compilation> 

    </system.web> 

    <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,單擊下一步

<a href="http://blog.51cto.com/attachment/201106/173717213.png" target="_blank"></a>

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

<a href="http://blog.51cto.com/attachment/201106/173841516.png" target="_blank"></a>

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

package myAssembly; 

import java.util.List; 

public class Test { 

   public static void main(String[] args){ 

       myServices.PersonService service=new myServices.PersonService(); 

       //擷取服務對象 

       myServices.IPersonService personService= 

           service.getBasicHttpBindingIPersonService(); 

       //通過basicHttpBinding協定綁定遠端對象 

       List&lt;myServices.Person&gt; personList=personService.getList().getPerson(); 

       for(int n=0;n&lt;personList.size();n++){ 

           System.out.println("ID:"+personList.get(n).getID()+ 

                   " Name:"+personList.get(n).getName().toString()+ 

                   " Age:"+personList.get(n).getAge()); 

       } 

   } 

二、使用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-&gt;Add XFire Web Service Capabilities,引用了XFire工具包以後。在項目會自動建立一個WebServices檔案夾,檔案夾裡面的service.xml就是對 釋出Web Service進行配置的。

現在先建立好一個服務層

//建立一個Model包,裡面包含一個值對象Person 

package Model; 

import java.io.Serializable; 

public class Person implements Serializable { 

    private int id; 

    private String name; 

    private int age; 

    public int getId(){ 

        return id; 

    public void setId(int id){ 

        this.id=id; 

    public String getName(){ 

        return name; 

    public void setName(String name){ 

        this.name=name; 

    public int getAge(){ 

        return age; 

    public void setAge(int age){ 

        this.age=age; 

//建立一個Service包,裡面包含服務接口 

package Service; 

import Model.*; 

public interface PersonService { 

   List&lt;Person&gt; GetList(); 

//建立一個ServiceImpl包,實作服務 

package ServiceImpl; 

import Service.*; 

import java.util.*; 

public class PersonServiceImpl implements PersonService{ 

   public List&lt;Person&gt; GetList(){ 

       List&lt;Person&gt; personList=new LinkedList&lt;Person&gt;(); 

       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上面對服務進行配置

&lt;?xml version="1.0" encoding="UTF-8"?&gt; 

&lt;beans xmlns="http://xfire.codehaus.org/config/1.0"&gt; 

&lt;service&gt; 

    &lt;name&gt;PersonService&lt;/name&gt; 

    &lt;namespace&gt;http://leslie-pc:8080/PersonService&lt;/namespace&gt; 

    &lt;serviceClass&gt; 

        Service.PersonService 

    &lt;/serviceClass&gt; 

    &lt;implementationClass&gt; 

        ServiceImpl.PersonServiceImpl 

    &lt;/implementationClass&gt; 

&lt;/service&gt; 

&lt;/beans&gt; 

其配置功能如下:

service

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

name

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

namespace

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

serviceClass

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

implemetationClass

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

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

&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 

- &lt;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"&gt; 

- &lt;wsdl:types&gt; 

- &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://leslie-pc:8080/PersonService"&gt; 

- &lt;xsd:element name="GetList"&gt; 

&lt;xsd:complexType /&gt; 

&lt;/xsd:element&gt; 

- &lt;xsd:element name="GetListResponse"&gt; 

- &lt;xsd:complexType&gt; 

- &lt;xsd:sequence&gt; 

&lt;xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns1:ArrayOfPerson" /&gt; 

&lt;/xsd:sequence&gt; 

&lt;/xsd:complexType&gt; 

&lt;/xsd:schema&gt; 

- &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://Model"&gt; 

- &lt;xsd:complexType name="ArrayOfPerson"&gt; 

&lt;xsd:element maxOccurs="unbounded" minOccurs="0" name="Person" nillable="true" type="ns1:Person" /&gt; 

- &lt;xsd:complexType name="Person"&gt; 

&lt;xsd:element minOccurs="0" name="age" type="xsd:int" /&gt; 

&lt;xsd:element minOccurs="0" name="id" type="xsd:int" /&gt; 

&lt;xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string" /&gt; 

&lt;/wsdl:types&gt; 

- &lt;wsdl:message name="GetListRequest"&gt; 

&lt;wsdl:part name="parameters" element="tns:GetList" /&gt; 

&lt;/wsdl:message&gt; 

- &lt;wsdl:message name="GetListResponse"&gt; 

&lt;wsdl:part name="parameters" element="tns:GetListResponse" /&gt; 

- &lt;wsdl:portType name="PersonServicePortType"&gt; 

- &lt;wsdl:operation name="GetList"&gt; 

&lt;wsdl:input name="GetListRequest" message="tns:GetListRequest" /&gt; 

&lt;wsdl:output name="GetListResponse" message="tns:GetListResponse" /&gt; 

&lt;/wsdl:operation&gt; 

&lt;/wsdl:portType&gt; 

- &lt;wsdl:binding name="PersonServiceHttpBinding" type="tns:PersonServicePortType"&gt; 

&lt;wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /&gt; 

&lt;wsdlsoap:operation soapAction="" /&gt; 

- &lt;wsdl:input name="GetListRequest"&gt; 

&lt;wsdlsoap:body use="literal" /&gt; 

&lt;/wsdl:input&gt; 

- &lt;wsdl:output name="GetListResponse"&gt; 

&lt;/wsdl:output&gt; 

&lt;/wsdl:binding&gt; 

- &lt;wsdl:service name="PersonService"&gt; 

- &lt;wsdl:port name="PersonServiceHttpPort" binding="tns:PersonServiceHttpBinding"&gt; 

&lt;wsdlsoap:address location="http://leslie-pc:8080/WebSite1/services/PersonService" /&gt; 

&lt;/wsdl:port&gt; 

&lt;/wsdl:service&gt; 

&lt;/wsdl:definitions&gt; 

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

protected void Page_Load(object sender, EventArgs e) 

    Service.PersonService personService = new Service.PersonService(); 

    IList&lt;Service.Person&gt; personList = personService.GetList(); 

    foreach(Service.Person person in personList) 

          Response.Write("id:" + person.id.ToString() + " name:" +  

           person.name + " age:" + person.age.ToString()+"&lt;br/&gt;"); 

測試成功的話,恭喜你,你已經了解到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項目,再加入原代碼即可以成功運作)

<a href="http://files.cnblogs.com/leslies2/Java%E6%9C%8D%E5%8A%A1%E5%99%A8.NET%E5%AE%A2%E6%88%B7%E7%AB%AF.rar" target="_blank">JAVA伺服器、.NET用戶端</a>

<a href="http://files.cnblogs.com/leslies2/.NET%E6%9C%8D%E5%8A%A1%E5%99%A8JAVA%E5%AE%A2%E6%88%B7%E7%AB%AF.rar" target="_blank">.NET伺服器、.JAVA用戶端</a>

<a href="http://www.cnblogs.com/leslies2/archive/2011/05/31/2052002.html">JAVA與.NET的互相調用——利用JNBridge橋接模式實作遠端通訊</a>

<a href="http://www.cnblogs.com/leslies2/archive/2011/05/03/2035399.html">JAVA與.NET的互相調用——TCP/IP套接字互相調用的基本架構(附原代碼)</a>

<a href="http://www.cnblogs.com/leslies2/archive/2011/04/24/2024707.html">JAVA與.NET的互相調用——通過Web服務實作互相調用(附原代碼)</a>

版權聲明:原創作品,如需轉載,請注明出處。否則将追究法律責任

本文轉自 leslies2  51CTO部落格,原文連結:http://blog.51cto.com/79100812/579939