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套接字进行相互调用的。