JAVA与.NET的相互调用――通过Web服务实现相互调用(附原代码)_第1页
JAVA与.NET的相互调用――通过Web服务实现相互调用(附原代码)_第2页
JAVA与.NET的相互调用――通过Web服务实现相互调用(附原代码)_第3页
JAVA与.NET的相互调用――通过Web服务实现相互调用(附原代码)_第4页
JAVA与.NET的相互调用――通过Web服务实现相互调用(附原代码)_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方。而在很多大型的开发项目里面,往往需要同时使用两种语言进行集成开发。而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候对另一种语言感觉到畏惧。在这里在下向各位介绍一下,JAVA与.NET相互调用的例子。下面的介绍主要包括三方面:一是通过常用Web服务进行相互调用,二是使用TCP/IP套接字进行相互调用,三是利用Remoting实现相互调用。在这章里面先为大家介绍一下最简单,最常用的Web服务相互调用方式。首先说一下Web服务的来源,Web服务是一种新的Web应用程序分支,可以执行从简单的请求到复杂商务处理等任

2、何功能。一旦部署以后,其他Web服务应用程序可以发现并调用它部署的服务。 Web Service是一种应用程序,它可以使用标准的互联网协议,像超文件传输协议(HTTP、简单对象访问协议(SOAP、XML等,将功能纲领性地体现在互联网和企业内部网上,Web服务被视作是Web上的组件编程。Web服务必须提供一套标准的类型系统,用于沟通不同平台、编程语言和组件模型中的不同类型系统。XML和X 可扩展的标记语言XML是Web Service平台中表示数据的基本格式。除了易于建立和易于分析外,XML主要的优点在于它既与平台无关,又与厂商无关。XML是由万维网协会 (W3C创建,W3C制定的XML Sch

3、emaXSD定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型。 Web Service平台是用XSD来作为数据类型系统的。当你用某种语言如JAVA、C#来构造一个Web Service时,为了符合Web Service标准,所有你使用的数据类型都必须被转换为XSD类型。如想让它使用在不同平台和不同软件的不同组织间传递,还需要通过SOAP协议将它包装起来。 SOAP SOAP即简单对象访问协议(Simple Object Access Protocol,它是用于交换XML编码信息的轻量级协议。它有三个主要方面:XML-envelope为描述信息内容和如何处理内容定义了框架,将程序对象

4、编码成为XML对象的规则,执行远程过程调用(RPC的约定。SOAP可以运行在任何其他传输协议上。例如,你可以使用 SMTP,即因特网电子邮件协议来传递SOAP消息,这可是很有诱惑力的。在传输层之间的头是不同的,但XML有效负载保持相同。Web Service 希望实现不同的系统之间能够用“软件-软件对话”的方式相互调用,打破了软件应用、网站和各种设备之间的格格不入的状态,实现“基于Web无缝集成”的目标。 WSDL Web Service描述语言WSDL就是用机器能阅读的方式提供的一个正式描述文档而基于XML的语言,用于描述Web Service及其函数、参数和返回值。因为是基于XML的,所以

5、WSDL既是机器可阅读的,又是人可阅读的。下面分开两个方面讲解一下如果通过Web服务实现JAVA与.NET的相互调用。一、使用.NET作为服务器端,JAVA作为客户端实现相互调用。 在.NET系统里面,以WCF作为新一代的服务开发工具是微软的一个新卖点,我们就以WCF为例子实现服务端,首先新建一个网站项目,在网站添加一个WCF服务PersonService。你将看到PersonService.svc、IPersonService、 PersonService.cs三个文件,其中IPersonService是对向暴露一个接口,接口的功能由PersonService来实现,客户端则通过Person

6、alService.svc来寻获服务,并对其添加引用的。c-sharp:collapse + expand sourceview plaincopyprint?1. /在PersonService.svc里,只包括一行,其中列明了对该服务的实现类   2. <% ServiceHost Language="C#" Debug="true" Service="Service.PersonService" CodeBehind="/App_Code/Pe

7、rsonService.cs" %>  3. /服务的实现   4. using System;  5. using System.Collections.Generic;  6. using System.Linq;  7. using System.Runtime.Serialization;  8. using System.ServiceModel;  9. using&#

8、160;System.Text;  10. namespace Service  11.   12. ServiceContract  13. public interface IPersonService  14.   15. OperationContract  16. IList  GetList(;   17.   18. public class 

9、PersonService : IPersonService  19.   20. public IList  GetList(   21.   22. IList  personList =  new List (;   23. Person person1 = new Person(;  24. person1.ID = 0;

10、0; 25. person1.Age = 27;  26. person1.Name = "Leslie"  27. personList.Add(person1;  28. Person person2 = new Person(;  29. person2.ID = 1;  30. person2.Age = 23;  31

11、. person2.Name = "Rose"  32. personList.Add(person2;  33. Person person3 = new Person(;  34. person3.ID = 2;  35. person3.Age = 29;  36. person3.Name = "Jack"  

12、37. personList.Add(person3;  38. return personList;  39.   40.   41.   为了使用Person能够实现远程传送,我们必须对Person进行序列化,在WCF中包括服务契约、数据契约、消息契约三部分,而数据契约正是用于对数据进行序列化处理的,如果想对WCF有进一步的了解,可以链接使用WCF实现SOA面向服务编程 c-sharp:collapse + expand sourceview plaincopyprint?1. u

13、sing System;  2. using System.Collections.Generic;  3. using System.Linq;  4. using System.Web;  5. using System.Runtime.Serialization;  6. /   7. /Person 的摘要说明   8. /    9. namesp

14、ace Service  10.   11. DataContract  12. public class Person  13.   14. DataMember  15. public int ID  16.   17. get;  18. set;  19.   20. DataMember  21.

15、public string Name  22.   23. get;  24. set;  25.   26. DataMember  27. public int Age  28.   29. get;  30. set;  31.   32.   33.   数据契约里面有多种的序列化方式,包括

16、 DataContractSerializer,NetDataContractSerializer,XmlServializer,DataContractJsonSerializer。在这里面只用使用最普遍的DataContractSerializer,而DataContractJsonSerializer是现今比较热门的使用方式,特别是在开发网络项目时候,多使用Json进行数据通讯。最后配置好web.config,就可以成功将WCF服务发布 c-sharp:collapse + expand sourceview plaincopyprint?1. "1.0"?>&

17、#160; 2.    3.    4.    5.    6. "serviceBehavior" >   7. "true" />  / 注意将 httpGetEnabled 设置为 true ,使客户端能够成功捕获服务    8. "false" />   9.   10.   11.   12

18、.    13. "Service.PersonService"  behaviorConfiguration= "serviceBehavior" >  /name 属性必须与服务实现类的类名相对应    14. ""  binding= "basicHttpBinding"  contract= "Service.IPersonService" />  /contract 必须与契约名相对应

19、    15. "mex"  binding= "mexHttpBinding"  contract= "IMetadataExchange" />  / 注意打开元数据,使客户能下载    16.   17.   18.   19.   下面使用MyEclipse8.6实现客户端开发,首先添加对服务的引用,按Ctrl+N新建一个项目,选择Web Service->Web S

20、ervice Client,单击下一步,这时候选择在Framework上选择JAX-WS,单击下一步 在WSDL URL上输入服务的路径,并为服务添加一个Java pagckage包myServices,点击完成,这样WCF服务便可成功加入到客户端。 此时为此项目添加测试类,运行进行测试java:collapse + expand sourceview plaincopyprint?1. package myAssembly;  2. import java.util.List;  3. public c

21、lass Test   4. public static void main(String args  5. myServices.PersonService service=new myServices.PersonService(; /获取服务对象   6. myServices.IPersonService personService=service.getBasicHttpBindingIPersonService(; 

22、;/通过basicHttpBinding协议绑定远程对象   7. List  personList=personService.getList(.getPerson(;   8. for(int n=0;n 9. System.out.println("ID:"+personList.get(n.getID(+" Name:"+personList.get(n.getName(.toString(+" Age:"+personList.get(n.get

23、Age(;  10.   11.   12.   二、使用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的调用转

24、换成为对于SOAP消息。 在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI (service endpoint interface,并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。在下面我们就以XFire建立一个Web Service。首先建立一个在一个项目上单击右键,选择MyEclipse->Add XFire Web Service Capabilities,引用了XFire工具包以后。在项目会自动建立一个WebServices文件夹,文件夹里面的service.xml就是对发布Web Service进行配置的。现

25、在先建立好一个服务层c-sharp:collapse + expand sourceview plaincopyprint?1. /建立一个Model包,里面包含一个值对象Person   2. package Model;  3. import java.io.Serializable;  4. public class Person implements Serializable   5. private int id;&

26、#160; 6. private String name;  7. private int age;  8. public int getId(  9. return id;  10.   11. public void setId(int id  12. this.id=id;  13.   14. public S

27、tring getName(  15. return name;  16.   17. public void setName(String name  18. =name;  19.   20. public int getAge(  21. return age;  22.   23. public voi

28、d setAge(int age  24. this.age=age;  25.   26.   27. /建立一个Service包,里面包含服务接口   28. package Service;  29. import java.util.List;  30. import Model.*;  31. public interface PersonService&#

29、160;  32. List  GetList(;   33.   34. /建立一个ServiceImpl包,实现服务   35. package ServiceImpl;  36. import Model.*;  37. import Service.*;  38. import java.util.*;  39. public class PersonServ

30、iceImpl implements PersonService  40. public List  GetList(   41. List  personList= new LinkedList (;   42. Person person1=new Person(;  43. person1.setId(0;  44. person1.setAge(23;  45. person1.setNa

31、me("Leslie"  46. personList.add(person1;  47. Person person2=new Person(;  48. person2.setId(1;  49. person2.setAge(30;  50. person2.setName("Mike"  51. personList.add(person2;  52. return person

32、List;  53.   54.   在Service.xml上面对服务进行配置java:collapse + expand sourceview plaincopyprint?1. "1.0" encoding="UTF-8"?>  2. "/config/1.0" >   3.    4. PersonService   5

33、. http: /leslie-pc:8080/PersonService   6.    7. Service.PersonService  8.   9.    10. ServiceImpl.PersonServiceImpl  11.   12.   13.   其配置功能如下: service service 标签和它所包含的 xml 内容为发布成 Web 服务的 POJO 提供完整的描述。 name We

34、b 服务被发布时所采用的唯一名称。 namespace Web 服务发布时所使用的命名空间。 serviceClass Web 服务接口类的全名,包括包名和类名。 implemetationClass Web 服务实现类的全名,包括包名和类名。现在可以运行程序,对服务进行测试,测试时输入服务地址http:/leslie-pc:8080/WebSite1/services/PersonService?wsdl,系统将显示wsdl代码xhtml:nogutter:collapse + expand sourceview plaincopyprint?1.  version="1

35、.0" encoding="UTF-8" ?>   2. -    targetNamespace = "http:/leslie-pc:8080/PersonService"   xmlns:ns1 = "http:/Model"   xmlns:soapenc12 = "/2003/05/soap-encoding"   xmlns:tns = "http

36、:/leslie-pc:8080/PersonService"   xmlns:wsdl = "/wsdl/"   xmlns:xsd = "/2001/XMLSchema"   xmlns:soap11 = "/soap/envelope/"   xmlns:wsdlsoap = "/wsdl/

37、soap/"   xmlns:soapen c11 = "/soap/encoding/"   xmlns:soap12 = "/2003/05/soap-envelope" >    3. -    4. -    xmlns:xsd = "/2001/XMLSchema"   attributeFormD

38、efault = "qualified"   elementFormDefault = "qualified"   targetNamespace = "http:/leslie-pc:8080/PersonService" >    5. -    name = "GetList" >    6.      />     7.   

39、60; 8. -    name = "GetListResponse" >    9. -    10. -    11.      maxOccurs = "1"   minOccurs = "1"   name = "out"   nillable = "true"   type = "ns1:A

40、rrayOfPerson"   />     12.     13.     14.     15.     16. -    xmlns:xsd = "/2001/XMLSchema"   attributeFormDefault = "qualified"   ele

41、mentFormDefault = "qualified"   targetNamespace = "http:/Model" >    17. -    name = "ArrayOfPerson" >    18. -    19.      maxOccurs = "unbounded"   minOccurs = "0"   n

42、ame = "Person"   nillable = "true"   type = "ns1:Person"   />     20.     21.     22. -    name = "Person" >    23. -    24.      minO

43、ccurs = "0"   name = "age"   type = "xsd:int"   />     25.      minOccurs = "0"   name = "id"   type = "xsd:int"   />     26.      minOccurs = &

44、quot;0"   name = "name"   nillable = "true"   type = "xsd:string"   />     27.     28.     29.     30.     31. -    name = "GetListR

45、equest" >    32.      name = "parameters"   element = "tns:GetList"   />     33.     34. -    name = "GetListResponse" >    35.      name = "param

46、eters"   element = "tns:GetListResponse"   />     36.     37. -    name = "PersonServicePortType" >    38. -    name = "GetList" >    39.      name = "

47、GetListRequest"   message = "tns:GetListRequest"   />     40.      name = "GetListResponse"   message = "tns:GetListResponse"   />     41.     42.     43.

48、 -    name = "PersonServiceHttpBinding"   type = "tns:PersonServicePortType" >    44.      style = "document"   mce_style = "document"   transport = "/soap/http"   /&g

49、t;     45. -    name = "GetList" >    46.      soapAction = ""   />     47. -    name = "GetListRequest" >    48.      use = "literal"   />     49.     50. -    name = "GetListResponse" >    51.      use = "literal"   />     52.     53.     54. 

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论