加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService CXF学习(入门篇2):HelloWorld示例

发布时间:2020-12-16 23:31:22 所属栏目:安全 来源:网络整理
导读:?理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloWorld Demo可来说明事更加直观。那下面咱们就开始进行讲解:? ?? 首先到apache官方网下载apache-cxf-2.2.2,(现在有更高版本2.4)地址:http://cxf.apache.org/? ?? 新建一个Java Project,导入cxf常

?理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloWorld Demo可来说明事更加直观。那下面咱们就开始进行讲解:?
?? 首先到apache官方网下载apache-cxf-2.2.2,(现在有更高版本2.4)地址:http://cxf.apache.org/?
?? 新建一个Java Project,导入cxf常用.jar包,cxf常用jar包如下:

1、commons-logging-1.1.1.jar
2、cxf-2.4.1.jar
3、geronimo-activation_1.1_spec-1.1.jar
4、geronimo-annotation_1.0_spec-1.1.1.jar
5、geronimo-javamail_1.4_spec-1.7.1.jar
6、geronimo-jaxws_2.2_spec-1.0.jar
7、geronimo-servlet_3.0_spec-1.0.jar
8、geronimo-stax-api_1.0_spec-1.0.1.jar
9、geronimo-ws-metadata_2.0_spec-1.1.3.jar
10、jettison-1.3.jar
11、jetty-continuation-7.4.2.v20110526.jar
12、jetty-http-7.4.2.v20110526.jar
13、jetty-io-7.4.2.v20110526.jar
14、jetty-server-7.4.2.v20110526.jar
15、jetty-util-7.4.2.v20110526.jar
16、neethi-3.0.0.jar
17、saaj-api-1.3.jar
18、saaj-impl-1.3.2.jar
19、serializer-2.7.1.jar
cxf结合spring时所需jar包
(
??? spring-asm-3.0.5.RELEASE.jar
??? spring-beans-3.0.5.RELEASE.jar
??? spring-context-3.0.5.RELEASE.jar
??? spring-core-3.0.5.RELEASE.jar
?? ?spring-expression-3.0.5.RELEASE.jar
??? spring-aop-3.0.5.RELEASE.jar
??? spring-web-3.0.5.RELEASE.jar
)
20、wsdl4j-1.6.2.jar
21、xalan-2.7.1.jar
22、xercesImpl.jar
23、xml-resolver-1.2.jar
24、xmlschema-core-2.0.jar
25、jaxb-api-2.2.1.jar? ---- webservices服务端需要加
26、jaxb-impl-2.2.1.1.jar ---- 如果jdk中的版本与该版本一致,则webservices服务端和客户端都不需要加
(注:jaxb-api和jaxb-impl包会和jdk中的冲突(jdk的版本<2.0>较低),在用这两个jar包的时候,可将2.2版本的jar覆盖jdk的版本)
覆盖jdk版本的方法: 找到jdk的安装目录下的jrelibendorsed文件夹(如果endorsed文件夹不存在,可新建),将jaxb-api-2.2.1和jaxb-impl-2.2.1.1放到此文件夹下即可。

第一步:新建一个webservice服务端接口和实现类

?1、服务端接口

[java]? view plain copy
  1. package?com.ws.services;??
  2. import?javax.jws.WebService;??
  3. ??
  4. @WebService??
  5. public?interface?IHelloServices?{??
  6. ????public?String?sayHelloToAll(String[]?userNames);??
  7. public?String[]?getHelloWords();??
  8. ??????
  9. public?String?sayHello(String?name);??
  10. }??

? 2、服务端接口实现类

copy
    package?com.ws.services.impl;??
  1. ??
  2. import?javax.jws.WebService;??
  3. import?com.ws.services.IHelloServices;??
  4. @WebService(endpointInterface="com.ws.services.IHelloServices")??
  5. class?HelloServicesImpl?implements?IHelloServices?{??
  6. public?String[]?getHelloWords()?{??
  7. ????????String[]?words?=?{"hello?vicky.","hello,i'm?vicky.","hi,ivy?and?simon."};??
  8. ????????return?words;??
  9. ????}??
  10. ????public?String?sayHello(String?name)?{??
  11. return?"hello?"+name+"?!?";??
  12. public?String?sayHelloToAll(String[]?userNames)?{??
  13. ????????String?hello?=?"hello?";??
  14. ????????for(int?i=0;i<userNames.length;i++){??
  15. ????????????if(i!=userNames.length-1)??
  16. ????????????????hello?+=?userNames[i]+"?and?";??
  17. else??
  18. ????????????????hello?+=?userNames[i]+"?.";??
  19. ????????}??
  20. return?hello;??
  21. ????}??
  22. }??

?3、创建webservices服务端,并发布服务

copy
    package?com.test;??
  1. import?javax.xml.ws.Endpoint;??
  2. import?org.apache.cxf.jaxws.JaxWsServerFactoryBean;??
  3. import?com.ws.services.impl.HelloServicesImpl;??
  4. import?com.ws.services.impl.UserServicesImpl;??
  5. class?ServerTest?{??
  6. public?ServerTest(){??
  7. ????????//?第一种发布方式??
  8. ????????IHelloServices?hello?=?new?HelloServicesImpl();??
  9. //?创建WebServices服务接口??
  10. ????????JaxWsServerFactoryBean?factory?=?new?JaxWsServerFactoryBean();??
  11. //?注册webservices接口??
  12. ????????factory.setServiceClass(IHelloServices.class);??
  13. //?发布接口??
  14. ????????factory.setAddress("http://localhost:8090/helloServices");??
  15. ????????factory.setServiceBean(hello);??
  16. ????????//?创建服务??
  17. ????????factory.create();??
  18. ??????????
  19. //?第二种发布方式??
  20. //Endpoint.publish("http://localhost:8090/helloServices",?new?HelloServicesImpl());??
  21. static?void?main(String[]?args)?{??
  22. //?启动服务??
  23. new?ServerTest();??
  24. ????????System.out.println("Server?ready...");?????
  25. try?{??
  26. ????????????Thread.sleep(1000*300);?//休眠五分分钟,便于测试?????
  27. ????????}?catch?(InterruptedException?e)?{??
  28. ????????????e.printStackTrace();??
  29. ????????}?????
  30. ????????System.out.println("Server?exit...");?????
  31. ????????System.exit(0);??
  32. }??


?

第二步:新建一个webservice客户端,并测试webServices的服务

??? 1、在本工程中测试(即服务端与客户端在同一个工程中)

copy
    package?com.ws.client;??
  1. import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
  2. import?com.ws.server.IHelloServices;??
  3. class?HelloTest?{??
  4. void?main(String[]?args)?{??
  5. //创建WebService客户端代理工厂?????
  6. ????????JaxWsProxyFactoryBean?factory?=?new?JaxWsProxyFactoryBean();?????
  7. //注册WebService接口?????
  8. ????????factory.setServiceClass(IHelloServices.class);?????
  9. //设置WebService地址?????
  10. ????????factory.setAddress("http://localhost:8090/helloServices");??????????
  11. ????????IHelloServices?iHelloWorld?=?(IHelloServices)factory.create();?????
  12. ????????System.out.println("invoke?webservice...");?????
  13. ????????String[]?hellos?=?iHelloWorld.getHelloWords();??
  14. 0;i<hellos.length;i++){??
  15. ????????????System.out.println(hellos[i]);??
  16. ????????}??
  17. 0);?????
  18. ????}????
  19. }??


???? 2、新建一个webservices客户端测试工程

???? (1)、新建一个Project,并加上cxf的jar包;

???? (2)、将Webservices服务端工程中的接口类Copy到客户端工程中,且路径要一直;

???? (3)、新建一个测试类,代码如上。

最后是万事俱备,只欠测试了? ??? 首先,运行服务端程序? ??? 其次,打开浏览器,在地址栏中输入http://localhost:8090/helloServices?wsdl(因为cxf自带了一个jetty服务器),查看接口是否发布成功,如里浏览器页面显示下面内容,证明接口发布成功 。 ??? 最后,运行客户程序。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读