1、Apache CXF实战Apache CXF实战Apache的CXF现在几乎成了Java领域构建Web Service的首选类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。当然首先想到的当然还是那个Hello World示例。这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容1. 3. 4.0.0 4. com.googlecode.garbagecan.cxfstudy 5. cxfstudy 6. war 7. 1.0-SNAPSHOT 8. cxfstudyMavenWebapp 9. http:/maven.apache.org 1
2、0. 11. 12. 2.2.7 13. 14. 15. 16. 17. org.apache.cxf 18. cxf-rt-frontend-jaxws 19. $cxf.version 20. 21. 22. org.apache.cxf 23. cxf-rt-transports-http 24. $cxf.version 25. 26. 27. org.apache.cxf 28. cxf-rt-transports-http-jetty 29. $cxf.version 30. 31. 32. org.apache.cxf 33. cxf-rt-ws-security 34. $cx
3、f.version 35. 36. 37. org.apache.cxf 38. cxf-rt-ws-policy 39. $cxf.version 40. 41. 42. org.apache.cxf 43. cxf-bundle-jaxrs 44. $cxf.version 45. 46. 47. javax.ws.rs 48. jsr311-api 49. 1.1.1 50. 51. 52. org.slf4j 53. slf4j-api 54. 1.5.8 55. 56. 57. org.slf4j 58. slf4j-jdk14 59. 1.5.8 60. 61. 62. commo
4、ns-httpclient 63. commons-httpclient 64. 3.0 65. 66. 67. commons-io 68. commons-io 69. 2.3 70. 71. 72. junit 73. junit 74. 4.8.1 75. test 76. 77. 78. 79. 80. cxfstudy 81. 82. 83. src/main/resources 84. 85. 86. src/main/java 87. 88. * 89. 90. 91. */*.java 92. 93. 94. 95. 96. 97. org.mortbay.jetty 98.
5、 maven-jetty-plugin 99. 100. / 101. 102. 103. 9000 104. 105. 106. 107. 108. 109. org.apache.maven.plugins 110. maven-compiler-plugin 111. 112. 1.5 113. 1.5 114. 115. 116. 117. 118. 119. 下面来看看HelloWorld的具体例子。 1.创建HelloWorld 接口类1. packagecom.googlecode.garbagecan.cxfstudy.helloworld; 2. 3. importjavax
6、.jws.WebMethod; 4. importjavax.jws.WebParam; 5. importjavax.jws.WebResult; 6. importjavax.jws.WebService; 7. 8. WebService 9. publicinterfaceHelloWorld 10. WebMethod 11. WebResultStringsayHi(WebParamStringtext); 12. 2.创建HelloWorld实现类 1. packagecom.googlecode.garbagecan.cxfstudy.helloworld; 2. 3. pub
7、licclassHelloWorldImplimplementsHelloWorld 4. 5. publicStringsayHi(Stringname) 6. Stringmsg=Hello+name+!; 7. returnmsg; 8. 9. 3.创建Server端测试类 1. packagecom.googlecode.garbagecan.cxfstudy.helloworld; 2. 3. importorg.apache.cxf.jaxws.JaxWsServerFactoryBean; 4. 5. /http:/localhost:9000/HelloWorld?wsdl 6
8、. publicclassServer 7. publicstaticvoidmain(Stringargs)throwsException 8. JaxWsServerFactoryBeanfactory=newJaxWsServerFactoryBean(); 9. factory.setServiceClass(HelloWorldImpl.class); 10. 11. factory.setAddress(http:/localhost:9000/ws/HelloWorld); 12. factory.create(); 13. 14. System.out.println(Serv
9、erstart.); 15. Thread.sleep(60*1000); 16. System.out.println(Serverexit.); 17. System.exit(0); 18. 19. 4.创建Client端测试类 1. packagecom.googlecode.garbagecan.cxfstudy.helloworld; 2. 3. importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean; 4. 5. publicclassClient 6. publicstaticvoidmain(Stringargs) 7. JaxWsP
10、roxyFactoryBeanfactory=newJaxWsProxyFactoryBean(); 8. factory.setServiceClass(HelloWorld.class); 9. factory.setAddress(http:/localhost:9000/ws/HelloWorld); 10. HelloWorldhelloworld=(HelloWorld)factory.create(); 11. System.out.println(helloworld.sayHi(kongxx); 12. System.exit(0); 13. 14. 5.测试 首先运行Ser
11、ver类来启动Web Service服务,然后访问http:/localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。运行Client测试类,会在命令行输出Hello kongxx!的message。书接上文,下面看看CXF怎样和spring集成。1.创建HelloWorld 接口类1. packagecom.googlecode.garbagecan.cxfstudy.helloworld;2. 3. importjavax.jws.WebMethod;4. importjavax.jws.WebParam;5. importjavax.j
12、ws.WebResult;6. importjavax.jws.WebService;7. 8. WebService9. publicinterfaceHelloWorld10. WebMethod11. WebResultStringsayHi(WebParamStringtext);12. 2.创建HelloWorld实现类 1. packagecom.googlecode.garbagecan.cxfstudy.helloworld;2. 3. publicclassHelloWorldImplimplementsHelloWorld4. 5. publicStringsayHi(St
13、ringname)6. Stringmsg=Hello+name+!;7. returnmsg;8. 9. 3.修改web.xml文件 1. 4. 5. 6. 7. cxfstudy8. 9. 10. cxf11. org.apache.cxf.transport.servlet.CXFServlet12. 113. 14. 15. 16. cxf17. /ws/*18. 19. 20. 21. org.springframework.web.context.ContextLoaderListener22. 23. 24. 25. contextConfigLocation26. classpath*:*/spring.xml27. 28. 29. 4.创建spring配置文件并放在classpath路径下 1. 2. 6. 7. 8. importresource=classpath:META-INF/cxf/cxf
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1