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"%>
//服務的實作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
// 注意: 使用“重構”菜單上的“重命名”指令,可以同時更改代碼和配置檔案中的接口名“IPersonService”。
namespace Service
{
[ServiceContract]
publicinterface IPersonService
[OperationContract]
IList<Person> GetList();
}
publicclass 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;

using System.Web;
///<summary>
///Person 的摘要說明
///</summary>
[DataContract]
publicclass Person
[DataMember]
publicint ID
get;
set;
publicstring Name
publicint Age
資料契約裡面有多種的序列化方式,包括DataContractSerializer,NetDataContractSerializer,XmlServializer,DataContractJsonSerializer。在這裡面隻用使用最普遍的DataContractSerializer,而DataContractJsonSerializer是現今比較熱門的方式,特别是在開發網絡項目時候,多使用Json進行資料通訊。
最後配置好web.config,就可以成功将WCF服務釋出

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>//注意将httpGetEnabled設定為true,使用戶端能夠成功捕獲服務
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.PersonService" behaviorConfiguration="serviceBehavior">//name屬性必須與服務實作類的類名相對應
<endpoint address="" binding="basicHttpBinding" contract="Service.IPersonService"/>//contract必須與契約名相對應
<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());
二、使用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進行配置的。
現在先建立好一個服務層

//建立一個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 Model.*;
publicinterface PersonService {
List<Person> GetList();
//建立一個ServiceImpl包,實作服務
package ServiceImpl;
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);
在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>
</beans>
其配置功能如下:
service
service 标簽和它所包含的 xml 内容為釋出成 Web 服務的 POJO 提供完整的描述。
name
Web 服務被釋出時所采用的唯一名稱。
namespace
Web 服務釋出時所使用的命名空間。
serviceClass
Web 服務接口類的全名,包括包名和類名。
implemetationClass
Web 服務實作類的全名,包括包名和類名。
現在可以運作程式,對服務進行測試,在測試時輸入服務位址http://leslie-pc:8080/WebSite1/services/PersonService?wsdl,系統将顯示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:schema>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://Model">
- <xsd:complexType name="ArrayOfPerson">
<xsd:element maxOccurs="unbounded" minOccurs="0" name="Person" nillable="true" type="ns1:Person"/>
- <xsd:complexType name="Person">
<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"/>
</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: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"/>
<wsdlsoap:operation soapAction=""/>
- <wsdl:input name="GetListRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
- <wsdl:output name="GetListResponse">
</wsdl:output>
</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套接字進行互相調用的。
原代碼: (由于上傳空間有限,未能将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>
原創作品,轉載時請注明作者及出處