首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

XFire构建web services客户端的5种方式

2012-08-24 
XFire构建web services客户端的五种方式本文主要源于先前看到的一篇文章,为了自己以后查看方便也就转载过

XFire构建web services客户端的五种方式

本文主要源于先前看到的一篇文章,为了自己以后查看方便也就转载过来了

原文链接:http://blog.csdn.net/jadyer/article/details/6082948

另外还有第四种,第五种方法,是从别处看到的 ,在本文最后和大家分享一下

?

这里并未涉及到JSR?181?Annotations?的相关应用,具体的三种方式如下
①?通过WSDL地址来创建动态客户端?
②?通过服务端提供的接口来创建客户端?
③?使用Ant通过WSDL文件来生成客户端


第一种方式:通过WSDL地址来创建动态客户端

view plainprint?
  1. package?com.jadyer.client;??
  2. import?java.net.MalformedURLException;??
  3. import?java.net.URL;??
  4. import?org.codehaus.xfire.client.Client;??
  5. /**?
  6. ?*?通过WSDL来创建动态客户端?
  7. ?*?@see?此时需要在项目中引入XFire?1.2?Core?Libraries和XFire?1.2?HTTP?Client?Libraries?
  8. ?*/??
  9. public?class?ClientFromWSDL?{??
  10. ????public?static?void?main(String[]?args)?throws?MalformedURLException,?Exception?{??
  11. ????????Client?client?=?new?Client(new?URL("http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl"));??
  12. ????????Object[]?results11?=?client.invoke("sayHello",?new?Object[]{"Jadyer22"});??
  13. ????????System.out.println(results11[0]);??
  14. ????}??
  15. }??

?

?

第二种方式:通过服务端提供的端口来创建客户端

view plainprint?
  1. package?com.jadyer.client;??
  2. import?java.net.MalformedURLException;??
  3. import?java.util.List;??
  4. import?org.codehaus.xfire.client.XFireProxyFactory;??
  5. import?org.codehaus.xfire.service.Service;??
  6. import?org.codehaus.xfire.service.binding.ObjectServiceFactory;??
  7. import?com.jadyer.model.Person;??
  8. import?com.jadyer.model.User;??
  9. import?com.jadyer.server.HelloService;??
  10. /**?
  11. ?*?通过Web服务端提供的接口来创建客户端?
  12. ?*?@see?客户端必须提供一个与服务端完全一致的接口,包名也要一致?
  13. ?*?@see?在本例中,需要在客户端(即该项目)中提供HelloService.java接口,以及Person和User两个POJO类?
  14. ?*?@see?并且此时需要在项目中引入XFire?1.2?Core?Libraries和XFire?1.2?HTTP?Client?Libraries?
  15. ?*/??
  16. public?class?ClientFromInterface?{??
  17. ????public?static?void?main(String[]?args)throws?MalformedURLException{??
  18. ????????//首先使用XFire的ObjectServiceFactory从HelloService接口创建一个服务模型serviceModel??
  19. ????????//serviceModel包含服务的说明,换句话说,就是服务的元数据??
  20. ????????//Create?a?metadata?of?the?service??
  21. ????????Service?serviceModel?=?new?ObjectServiceFactory().create(HelloService.class);??
  22. ??????????
  23. ????????//访问的地址??
  24. ????????String?serviceURL?=?"http://127.0.0.1:8080/XFire_demo/services/XFireServer";??
  25. ??????????
  26. ????????//通过查看org.codehaus.xfire.client.XFireProxyFactory源码发现??
  27. ????????//下面两行代码与这里直接new?XFireProxyFactory()的作用是等效的??
  28. ????????//XFire?xfire?=?XFireFactory.newInstance().getXFire();??
  29. ????????//XFireProxyFactory?factory?=?new?XFireProxyFactory(xfire);??
  30. ????????//为XFire获得一个代理工厂对象??
  31. ????????//Create?a?proxy?for?the?deployed?service??
  32. ????????XFireProxyFactory?factory?=?new?XFireProxyFactory();??
  33. ??????????
  34. ????????//通过proxyFactory,使用服务模型serviceModel和服务端点URL(用来获得WSDL)??
  35. ????????//得到一个服务的本地代理,这个代理就是实际的客户端??
  36. ????????HelloService?client?=?(HelloService)factory.create(serviceModel,?serviceURL);??
  37. ??????????
  38. ????????/**?
  39. ?????????*?Invoke?the?service?
  40. ?????????*?@see?调用服务的本地代理(即实际的客户端)中的方法,便得到我们需要的WebServcie?
  41. ?????????*/??
  42. ??????????
  43. ????????/*--处理简单对象--*/??
  44. ????????String?serviceResponse?=?client.sayHello("Jadyer11");??
  45. ????????System.out.println(serviceResponse);??
  46. ??????????
  47. ????????/*--处理对象--*/??
  48. ????????User?u?=?new?User();??
  49. ????????u.setName("Jadyer99");??
  50. ????????Person?pp?=?client.getPerson(u);??
  51. ????????System.out.println(pp.getName());??
  52. ??????????
  53. ????????/*--处理List--*/??
  54. ????????List<Person>?personList?=?client.getPersonList(24,?"Jadyer88");??
  55. ????????for(Person?p?:?personList){??
  56. ????????????System.out.println(p.getName());??
  57. ????????}??
  58. ????}??
  59. }??

?

这是它要用到的接口和两个POJO类

view plainprint?
  1. /**?
  2. ?*?Web服务提供给客户端的接口?
  3. ?*?@see?这是第二种方式创建的客户端,要用到的接口?
  4. ?*/??
  5. package?com.jadyer.server;??
  6. import?java.util.List;??
  7. import?com.jadyer.model.Person;??
  8. import?com.jadyer.model.User;??
  9. public?interface?HelloService?{??
  10. ????public?String?sayHello(String?name);??
  11. ????public?Person?getPerson(User?u);??
  12. ????public?List<Person>?getPersonList(Integer?age,?String?name);??
  13. }??
  14. /**?
  15. ?*?第二种方式创建的客户端,要用到的两个POJO类?
  16. ?*/??
  17. package?com.jadyer.model;??
  18. public?class?User?{??
  19. ????private?String?name;??
  20. ????/*--getter和setter略--*/??
  21. }??
  22. package?com.jadyer.model;??
  23. public?class?Person?{??
  24. ????private?Integer?age;??
  25. ????private?String?name;??
  26. ????/*--getter和setter略--*/??
  27. }??

?

第三种方式:使用Ant通过WSDL文件来生成客户端

view plainprint?
  1. package?com.jadyer.client;??
  2. /**?
  3. ?*?使用Ant通过WSDL生成客户端?
  4. ?*?@see?这里的ClientFromAnt.java是我自己创建的,并非Ant生成?
  5. ?*?@see?这里要用到的JAR有:xfire-all-1.2.6.jar以及//xfire-distribution-1.2.6//lib//目录中的所有JAR包?
  6. ?*?@see?我们需要把这些JAR包都拷贝到Web?Project//WebRoot//WEB-INF//lib//目录中?
  7. ?*?@see?然后把build.xml和MyFirstXFireServer.wsdl都拷贝到下Web?Project的根目录下即可?
  8. ?*?@see?关于MyFirstXFireServer.wsdl文件,是我在WebServices服务启动后?
  9. ?*?@see?访问http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl然后将其另存得到的?
  10. ?*/??
  11. public?class?ClientFromAnt?{??
  12. ????public?static?void?main(String[]?args)?{??
  13. ????????XFireServerClient?client?=?new?XFireServerClient();??
  14. ????????//String?url?=?"http://127.0.0.1:8080/XFire_demo/services/XFireServer";??
  15. ????????//String?result?=?client.getXFireServerHttpPort(url).sayHello("Jadyer33");??
  16. ????????//上面的两行代码,与下面的这一行代码,同效~~??
  17. ????????String?result?=?client.getXFireServerHttpPort().sayHello("Jadyer33");??
  18. ??????????
  19. ????????System.out.println(result);??
  20. ????}??
  21. }??

?

用到的Ant文件,如下

view plainprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <project?name="wsgen"?default="wsgen"?basedir=".">??
  3. ????<path?id="classpathId">??
  4. ????????<fileset?dir="./WebRoot/WEB-INF/lib">??
  5. ????????????<include?name="*.jar"?/>??
  6. ????????</fileset>??
  7. ????</path>??
  8. ??????
  9. ????<taskdef?classpathref="classpathId"?name="wsgen"?classname="org.codehaus.xfire.gen.WsGenTask"/>??
  10. ??????
  11. ????<target?name="wsgen"?description="generate?client">??
  12. ????????<wsgen?outputDirectory="./src/"?wsdl="MyFirstXFireServer.wsdl"?binding="xmlbeans"?package="com.jadyer.client"?overwrite="true"/>??
  13. ????</target>??
  14. </project>??

?

也可以使用下面的这个Ant文件

view plainprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <project?name="xfireAnt"?basedir="."?default="createClientCode">??
  3. ????<property?name="xfirelib"?value="${basedir}/WebRoot/WEB-INF/lib"/>??
  4. ????<property?name="sources"?value="${basedir}/src"/>??
  5. ????<path?id="classpath">??
  6. ????????<fileset?dir="${xfirelib}">??
  7. ????????????<include?name="*.jar"/>??
  8. ????????</fileset>??
  9. ????</path>??
  10. ??????????
  11. ????<target?name="createClientCode">??
  12. ????????<taskdef?name="wsgen"?classname="org.codehaus.xfire.gen.WsGenTask"?classpathref="classpath"/>??
  13. ????????<wsgen?outputDirectory="${sources}"?wsdl="http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl"?package="com.jadyer.client"?overwrite="true"/>??
  14. ????</target>??
  15. </project>??

?

最后我再把MyFirstXFireServer.wsdl的内容,附加上

view plainprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <wsdl:definitions?targetNamespace="http://www.jadyer.com/XFireDemo"??
  3. ????xmlns:tns="http://www.jadyer.com/XFireDemo"??
  4. ????xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"??
  5. ????xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"??
  6. ????xmlns:xsd="http://www.w3.org/2001/XMLSchema"??
  7. ????xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"??
  8. ????xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"??
  9. ????xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"??
  10. ????xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">??
  11. ????<wsdl:types>??
  12. ????????<xsd:schema?xmlns:xsd="http://www.w3.org/2001/XMLSchema"??
  13. ????????????????????attributeFormDefault="qualified"??
  14. ????????????????????elementFormDefault="qualified"??
  15. ????????????????????targetNamespace="http://www.jadyer.com/XFireDemo">??
  16. ????????????<xsd:element?name="sayHello">??
  17. ????????????????<xsd:complexType>??
  18. ????????????????????<xsd:sequence>??
  19. ????????????????????????<xsd:element?maxOccurs="1"?minOccurs="1"?name="in0"?nillable="true"?type="xsd:string"?/>??
  20. ????????????????????</xsd:sequence>??
  21. ????????????????</xsd:complexType>??
  22. ????????????</xsd:element>??
  23. ????????????<xsd:element?name="sayHelloResponse">??
  24. ????????????????<xsd:complexType>??
  25. ????????????????????<xsd:sequence>??
  26. ????????????????????????<xsd:element?maxOccurs="1"?minOccurs="1"?name="out"?nillable="true"?type="xsd:string"?/>??
  27. ????????????????????</xsd:sequence>??
  28. ????????????????</xsd:complexType>??
  29. ????????????</xsd:element>??
  30. ????????</xsd:schema>??
  31. ????</wsdl:types>??
  32. ????<wsdl:message?name="sayHelloRequest">??
  33. ????????<wsdl:part?name="parameters"?element="tns:sayHello"></wsdl:part>??
  34. ????</wsdl:message>??
  35. ????<wsdl:message?name="sayHelloResponse">??
  36. ????????<wsdl:part?name="parameters"?element="tns:sayHelloResponse"></wsdl:part>??
  37. ????</wsdl:message>??
  38. ????<wsdl:portType?name="XFireServerPortType">??
  39. ????????<wsdl:operation?name="sayHello">??
  40. ????????????<wsdl:input?name="sayHelloRequest"?message="tns:sayHelloRequest">??
  41. ????????????</wsdl:input>??
  42. ????????????<wsdl:output?name="sayHelloResponse"?message="tns:sayHelloResponse">??
  43. ????????????</wsdl:output>??
  44. ????????</wsdl:operation>??
  45. ????</wsdl:portType>??
  46. ????<wsdl:binding?name="XFireServerHttpBinding"?type="tns:XFireServerPortType">??
  47. ????????<wsdlsoap:binding?style="document"?mce_style="document"?transport="http://schemas.xmlsoap.org/soap/http"?/>??
  48. ????????<wsdl:operation?name="sayHello">??
  49. ????????????<wsdlsoap:operation?soapAction=""?/>??
  50. ????????????<wsdl:input?name="sayHelloRequest">??
  51. ????????????????<wsdlsoap:body?use="literal"?/>??
  52. ????????????</wsdl:input>??
  53. ????????????<wsdl:output?name="sayHelloResponse">??
  54. ????????????????<wsdlsoap:body?use="literal"?/>??
  55. ????????????</wsdl:output>??
  56. ????????</wsdl:operation>??
  57. ????</wsdl:binding>??
  58. ????<wsdl:service?name="XFireServer">??
  59. ????????<wsdl:port?name="XFireServerHttpPort"?binding="tns:XFireServerHttpBinding">??
  60. ????????????<wsdlsoap:address?location="http://127.0.0.1:8080/XFire_demo/services/XFireServer"?/>??
  61. ????????</wsdl:port>??
  62. ????</wsdl:service>??
  63. </wsdl:definitions>??

第四种方法

这种方法用到了spring的jar包,是前几天在找XFire+Spring的资料的时候看到的,在这里也是做个记录。同样的,这种方法和上面所提到的第二种方法在客户端都需要与服务器一样的接口,包名也必须一样。

(1)在src目录下新建client.xml(名字并非特定)

?

Java代码??XFire构建web services客户端的5种方式
  1. <span?style="font-family:?'Microsoft?YaHei';"><?xml?version="1.0"?encoding="UTF-8"?>????
  2. <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">????
  3. <beans>????
  4. ????<bean?id="baseService"?class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean"?lazy-init="false"?abstract="true"/>??
  5. <!--????id的名字作为标识,用于客户端程序中获取service,若有多个service咋在下面添加多个bean即可-->??
  6. ????<bean?id="MathService"?parent="baseService">??
  7. ????????<property?name="serviceClass">??
  8. ????????????<value>service.MathService</value>??
  9. ????????</property>??
  10. ????????<property?name="wsdlDocumentUrl">??
  11. ????????????<value>http://localhost:8080/myservice/mathWebService?wsdl</value>??
  12. ????????</property>??
  13. ????</bean>??
  14. </beans>?</span>??

(2)在程序中调用服务代码非常简单

?

?

Java代码??XFire构建web services客户端的5种方式
  1. ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("client.xml");??
  2. ??
  3. MathService?mathService?=?(MathService)ctx.getBean("MathService");??
  4. int?result?=?mathService.add(int?one,int?two);??

?

第五种办法

?

先获取到wsdl文件,命名为mathWebService.wsdl放在客户端的src目录下,接着通过程序访问该wsdl文件,并调用需要的方法。

?

Java代码??XFire构建web services客户端的5种方式
  1. String?wsdl??=???"mathWebService.wsdl?"?;??//?对应的WSDL文件???
  2. ???????Resource?resource??=???new??ClassPathResource(wsdl);???
  3. ???????Client?client??=???new??Client(resource.getInputStream(),??null?);??//?根据WSDL创建客户实例???
  4. ?????????
  5. ???????Object[]?objArray??=???new??Object[?2?];??
  6. ???????objArray[?0?]??=???2?;??
  7. ???????obiArray[1]?=?3;??
  8. ????????//?调用特定的Web?Service方法???
  9. ???????Object[]?results??=??client.invoke(?"?add?"?,?objArray);??
  10. ???????System.out.println(?"?result:??"???+??results[?0?]);??

?

对于这几种方法,第一种方法如果传递的参数为服务器端的实体对象,这点好像比较麻烦,不知道在客户端建立和服务器端相同的实体类行不行,没有实践,返回结果如果是复杂数据类型的话不知道有没有什么问题,或者如何转换,没有深入研究。而且我个人觉得方法调用不是那么直观。第三种方法里面

  1. wsdl="http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl"?

?

这个地方我不知道如何添加多个链接,有知道的朋友希望能指明一下,谢谢了。如果只能有这一个服务,那就太囧了。其他的方法倒是不觉得有什么大问题,性能什么的差异并未关注,欢迎大家讨论。

热点排行