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來尋獲服務,并對其添加引用的。
<%@ 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]
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能夠實現遠程傳送,我們必須對Person進行序列化,在WCF中包括服務契約、數據契約、消息契約三部分,而數據契約正是用于對數據進行序列化處理的,如果想對WCF有進一步的了解,可以鏈接使用WCF實現SOA面向服務編程:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
/// <summary>
///Person 的摘要說明
/// </summary>
namespace Service
{
[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服務發布:
<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服務便可成功加入到客戶端。
此時為此項目添加測試類,運行進行測試:
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<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進行配置的。
現在先建立好一個服務層:
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 java.util.List;
import Model.*;
public interface PersonService {
List<Person> GetList();
}
//建立一個ServiceImpl包,實現服務
package ServiceImpl;
import Model.*;
import Service.*;
import java.util.*;
public class 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上面對服務進行配置:
<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代碼:
- <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 ,在一個頁面上輸入代碼進行測試。
{
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項目,再加入原代碼即可以成功運行)