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;
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>
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>
targetnamespace="http://model">
- <xsd:complextype
name="arrayofperson">
maxoccurs="unbounded" minoccurs="0" name="person"
nillable="true" type="ns1:person"/>
name="person">
minoccurs="0" name="age" type="xsd:int"/>
minoccurs="0" name="id" type="xsd:int"/>
minoccurs="0" name="name" nillable="true"
type="xsd:string"/>
</wsdl:types>
- <wsdl:message
name="getlistrequest">
<wsdl:part
name="parameters" element="tns:getlist"/>
</wsdl:message>
name="parameters" element="tns:getlistresponse"/>
- <wsdl:porttype
name="personserviceporttype">
- <wsdl:operation
<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
<wsdlsoap:body
use="literal"/>
</wsdl:input>
- <wsdl:output
</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套接字進行互相調用的。