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

CXF实现webService服务(一)

发布时间:2020-12-16 22:44:49 所属栏目:安全 来源:网络整理
导读:以前工作中也用CXF,但都是用别人现成搭好的环境,这次自己重头搭建一遍环境。过程中也有遇到的问题,也做了简单的整理。 对于CXF是干什么用的,我不想多说,大家都知道这是我们在java编程中webService技术的一种实现工具。我们说说为什么用CXF来实现webServi

以前工作中也用CXF,但都是用别人现成搭好的环境,这次自己重头搭建一遍环境。过程中也有遇到的问题,也做了简单的整理。

对于CXF是干什么用的,我不想多说,大家都知道这是我们在java编程中webService技术的一种实现工具。我们说说为什么用CXF来实现webService:

1.??????Java的webService实现本身就是一个很耗性能的实现方案(xml与java对象之间在服务端以及客户端的互转比较消耗性能)

2.??????目前java主流的webService应用以CXF、AXIS2为主;

3.??????通过网络渠道的了解,目前CXF的效率要比AXIS2高出至少50%;

4.??????另外有一个webService的工具metro的效率比CXF高出10%;

5.??????CXF的实现资料网上可以随便找出一大堆,metro的资料相对少一些;

6.??????CXF在java应用实现中已经很成熟,企业更倾向于用这样一个成熟的解决方案;

基于以上原因,我选择CXF来实现webService。

参考资料:

Java Web 服务: CXF 性能比较----CXF 与最新版本的 Axis2 和 Metro 之间的性能对比

http://www.ibm.com/developerworks/cn/java/j-jws14/

?

一 ? 以annotation注解方式实现发布webService应用

1、? 基础环境

新建java web工程cxf之后,下载cxf工具包。解压CXF之后,把cxf工具包lib下的jar包全部放到工程的lib下。

此处用到的cxf工具包版本为:apache-cxf-2.7.12

下载地址:

http://www.apache.org/dyn/closer.cgi?path=/cxf/2.7.12/apache-cxf-2.7.12.zip

?

2、? 编写服务接口

见文件HelloWorld.java

[java] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. package?com.hsy.server;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?javax.jws.WebParam;??
  6. import?javax.jws.WebService;??
  7. ??
  8. import?com.hsy.pojo.User;??
  9. ??
  10. @WebService??
  11. public?interface?HelloWorld?{??
  12. ????String?sayHi(@WebParam(name="text")String?text);??
  13. ????String?sayHiToUser(User?user);??
  14. ????String[]?SayHiToUserList(List<User>?userList);??
  15. }??


?

3、? 服务接口实现

见文件HelloWorldImpl.java

[java] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. package?com.hsy.server;??
  2. ??
  3. import?java.util.LinkedHashMap;??
  4. import?java.util.List;??
  5. import?java.util.Map;??
  6. ??
  7. import?javax.jws.WebParam;??
  8. import?javax.jws.WebService;??
  9. ??
  10. import?com.hsy.pojo.User;??
  11. ??
  12. @WebService(endpointInterface="com.hsy.server.HelloWorld",serviceName="HelloWorld")??
  13. public?class?HelloWorldImpl?implements?HelloWorld?{??
  14. ????Map<Integer,?User>?users?=?new?LinkedHashMap<Integer,?User>();??
  15. ??
  16. ????public?String?sayHi(@WebParam(name?=?"text")?String?text)?{??
  17. ????????return?"Hello,"+text;??
  18. ????}??
  19. ??
  20. ????public?String?sayHiToUser(User?user)?{??
  21. ????????users.put(users.size()+1,?user);??
  22. ????????return?"Hello,"+user.getName();??
  23. ????}??
  24. ??
  25. ????public?String[]?SayHiToUserList(List<User>?userList)?{??
  26. ????????String[]?result?=?new?String[userList.size()];??
  27. ????????int?i?=?0;??
  28. ????????for(User?u:userList){??
  29. ????????????result[i]?=?"Hello?"?+?u.getName();??
  30. ????????????i++;??
  31. ????????}??
  32. ????????return?result;??
  33. ????}??
  34. ??
  35. ????/**?
  36. ?????*?@param?args?
  37. ?????*/??
  38. ????public?static?void?main(String[]?args)?{??
  39. ????????//?TODO?Auto-generated?method?stub??
  40. ??
  41. ????}??
  42. ??
  43. }??

4、? 发布服务app

见文件webServiceApp.java

[java] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. package?com.hsy.server;??
  2. ??
  3. import?javax.xml.ws.Endpoint;??
  4. ??
  5. public?class?webServiceApp?{??
  6. ??
  7. ????/**?
  8. ?????*?@param?args?
  9. ?????*/??
  10. ????public?static?void?main(String[]?args)?{??
  11. ?????????System.out.println("web?service?start");??
  12. ?????????HelloWorldImpl?implementor?=?new?HelloWorldImpl();??
  13. ?????????String?address?=?"http://localhost:8080/helloWorld";??
  14. ?????????Endpoint.publish(address,?implementor);??
  15. ?????????System.out.println("web?service?started");??
  16. ????}??
  17. ??
  18. }??



右键 run as 选择java application发布服务;然后在浏览器输入地址:http://localhost:8080/helloWorld?wsdl

如图:20140805132120.jpg


说明webService服务发布成功。

?

5、? 客户端访问服务

见文件HelloWorldClient.java

[java] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. package?com.hsy.client;??
  2. ??
  3. import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
  4. ??
  5. import?com.hsy.pojo.User;??
  6. import?com.hsy.server.HelloWorld;??
  7. ??
  8. public?class?HelloWorldClient?{??
  9. ??
  10. ????/**?
  11. ?????*?@param?args?
  12. ?????*/??
  13. ????public?static?void?main(String[]?args)?{??
  14. ??????????
  15. ????????//首先右键run?as?运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码??
  16. ????????JaxWsProxyFactoryBean?jwpfb?=?new?JaxWsProxyFactoryBean();??
  17. ????????jwpfb.setServiceClass(HelloWorld.class);??
  18. ????????jwpfb.setAddress("http://localhost:8080/helloWorld");??
  19. ????????HelloWorld?hw?=?(HelloWorld)?jwpfb.create();??
  20. ????????User?user?=?new?User();??
  21. ????????user.setName("马克思");??
  22. ????????user.setDescription("怀念马克思");??
  23. ????????System.out.println(hw.sayHiToUser(user));??
  24. ??????????
  25. ????}??
  26. ??
  27. }??


右键 run as 选择java application,控制台打印如图:

20140805132610.jpg


Ok,客户端访问也成功了。

6、? 附:

User.java

[java] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. package?com.hsy.pojo;??
  2. ??
  3. import?java.io.Serializable;??
  4. ??
  5. @SuppressWarnings("serial")??
  6. public?class?User?implements?Serializable?{??
  7. ??
  8. ????private?String?id;??
  9. ????private?String?name;??
  10. ????private?String?age;??
  11. ????private?String?description;??
  12. ??????
  13. ????public?User()?{??
  14. ????????super();??
  15. ????}??
  16. ??
  17. ????public?String?getId()?{??
  18. ????????return?id;??
  19. ????}??
  20. ??
  21. ????public?void?setId(String?id)?{??
  22. ????????this.id?=?id;??
  23. ????}??
  24. ??
  25. ????public?String?getName()?{??
  26. ????????return?name;??
  27. ????}??
  28. ??
  29. ????public?void?setName(String?name)?{??
  30. ????????this.name?=?name;??
  31. ????}??
  32. ??
  33. ????public?String?getAge()?{??
  34. ????????return?age;??
  35. ????}??
  36. ??
  37. ????public?void?setAge(String?age)?{??
  38. ????????this.age?=?age;??
  39. ????}??
  40. ??
  41. ????public?String?getDescription()?{??
  42. ????????return?description;??
  43. ????}??
  44. ??
  45. ????public?void?setDescription(String?description)?{??
  46. ????????this.description?=?description;??
  47. ????}??
  48. ??????
  49. ??????
  50. }??


?

二与spring集成实现webService

1、? 配置web.xml

见文件web.xml

[html] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?id="WebApp_ID"?version="2.5">??
  3. ??<display-name>cxf</display-name>??
  4. ??<welcome-file-list>??
  5. ????<welcome-file>index.html</welcome-file>??
  6. ????<welcome-file>index.htm</welcome-file>??
  7. ????<welcome-file>index.jsp</welcome-file>??
  8. ????<welcome-file>default.html</welcome-file>??
  9. ????<welcome-file>default.htm</welcome-file>??
  10. ????<welcome-file>default.jsp</welcome-file>??
  11. ??</welcome-file-list>??
  12. ????
  13. ????<context-param>??
  14. ????????<param-name>contextConfigLocation</param-name>??
  15. ????????<param-value>WEB-INF/classes/applicationContext.xml</param-value>??
  16. ????</context-param>??
  17. ??
  18. ????<listener>??
  19. ????????<listener-class>??
  20. ??????????????org.springframework.web.context.ContextLoaderListener??
  21. ????????</listener-class>??
  22. ????</listener>??
  23. ??
  24. ????<servlet>??
  25. ????????<servlet-name>CXFServlet</servlet-name>??
  26. ????????<servlet-class>??
  27. ???????????????org.apache.cxf.transport.servlet.CXFServlet??
  28. ????????</servlet-class>??
  29. ????????<load-on-startup>1</load-on-startup>??
  30. ????</servlet>??
  31. ??
  32. ????<servlet-mapping>??
  33. ?????????<servlet-name>CXFServlet</servlet-name>??
  34. ?????????<url-pattern>/webservice/*</url-pattern>??
  35. ????</servlet-mapping>??
  36. ????
  37. ????
  38. ????
  39. ????
  40. ??<!--?字符过滤器?-->????
  41. ????<filter>????
  42. ????????<filter-name>encoding</filter-name>????
  43. ????????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>????
  44. ????????<init-param>????
  45. ????????????<param-name>encoding</param-name>????
  46. ????????????<param-value>UTF-8</param-value>????
  47. ????????</init-param>????
  48. ????????<init-param>????
  49. ????????????<param-name>forceEncoding</param-name>????
  50. ????????????<param-value>true</param-value>????
  51. ????????</init-param>????
  52. ????</filter>????
  53. ????????
  54. ????????
  55. ????<filter-mapping>????
  56. ????????<filter-name>encoding</filter-name>????
  57. ????????<url-pattern>*.jsp</url-pattern>????
  58. ????</filter-mapping>????
  59. ????<filter-mapping>????
  60. ????????<filter-name>encoding</filter-name>????
  61. ????????<url-pattern>*.html</url-pattern>????
  62. ????</filter-mapping>????
  63. ????<filter-mapping>????
  64. ????????<filter-name>encoding</filter-name>????
  65. ????????<url-pattern>*.do</url-pattern>????
  66. ????</filter-mapping>????
  67. ????<filter-mapping>????
  68. ????????<filter-name>encoding</filter-name>????
  69. ????????<url-pattern>*.action</url-pattern>????
  70. ????</filter-mapping>???
  71. ????<filter-mapping>????
  72. ????????<filter-name>encoding</filter-name>????
  73. ????????<url-pattern>*.jsp</url-pattern>????
  74. ????</filter-mapping>????
  75. ????<filter-mapping>????
  76. ????????<filter-name>encoding</filter-name>????
  77. ????????<url-pattern>*.html</url-pattern>????
  78. ????</filter-mapping>????
  79. ????<filter-mapping>????
  80. ????????<filter-name>encoding</filter-name>????
  81. ????????<url-pattern>*.do</url-pattern>????
  82. ????</filter-mapping>????
  83. ????<filter-mapping>????
  84. ????????<filter-name>encoding</filter-name>????
  85. ????????<url-pattern>*.3g</url-pattern>????
  86. ????</filter-mapping>?????
  87. ????
  88. </web-app>??


?

2、? 配置applicationContext.xml

见文件applicationContext.xml

[html] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ???????xmlns:jaxws="http://cxf.apache.org/jaxws"??
  5. ???????xsi:schemaLocation="??
  6. ?????????????http://www.springframework.org/schema/beans??
  7. ?????????????http://www.springframework.org/schema/beans/spring-beans.xsd??
  8. ?????????????http://cxf.apache.org/jaxws???
  9. ?????????????http://cxf.apache.org/schemas/jaxws.xsd">??
  10. ??
  11. ??????<import?resource="classpath:META-INF/cxf/cxf.xml"/>??
  12. ??????<import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>??
  13. ??????<import?resource="classpath:META-INF/cxf/cxf-servlet.xml"/>??
  14. ??
  15. ??????<jaxws:endpoint???
  16. ?????????????id="helloWorld"??
  17. ?????????????implementor="com.hsy.server.HelloWorldImpl"??
  18. ?????????????address="/helloWorld"?/>??
  19. ??
  20. ?????<bean?id="client"???
  21. ????????????class="com.hsy.server.HelloWorld"???
  22. ????????????factory-bean="clientFactory"???
  23. ????????????factory-method="create"/>??
  24. ??
  25. ?????<bean?id="clientFactory"?class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">??
  26. ????????????<property?name="serviceClass"?value="com.hsy.server.HelloWorld"/>??
  27. ????????????<property?name="address"?value="http://localhost:8080/cxf/webservice/helloWorld"/>??
  28. ?????</bean>??
  29. ???????
  30. </beans>??


?

3、? 修改客户端代码

见文件HelloWorldClient.java

[java] view plain copy

在CODE上查看代码片

派生到我的代码片

  1. package?com.hsy.client;??
  2. ??
  3. import?java.util.ArrayList;??
  4. import?java.util.List;??
  5. ??
  6. import?org.springframework.beans.factory.BeanFactory;??
  7. import?org.springframework.beans.factory.xml.XmlBeanFactory;??
  8. import?org.springframework.context.ApplicationContext;??
  9. import?org.springframework.context.support.ClassPathXmlApplicationContext;??
  10. import?org.springframework.core.io.FileSystemResource;??
  11. import?org.springframework.core.io.Resource;??
  12. ??
  13. import?com.hsy.pojo.User;??
  14. import?com.hsy.server.HelloWorld;??
  15. ??
  16. public?class?HelloWorldClient?{??
  17. ??
  18. ????/**?
  19. ?????*?@param?args?
  20. ?????*/??
  21. ????public?static?void?main(String[]?args)?{??
  22. ??????????
  23. ????????//Resource?resource=?new?FileSystemResource("F:/workspaces4me2013/.metadata/.me_tcat/WEB-INF/classes/applicationContext.xml");?????
  24. ????????//BeanFactory?factory=?new?XmlBeanFactory(resource?);???
  25. ????????ApplicationContext?factory?=?new?ClassPathXmlApplicationContext("/applicationContext.xml");??
  26. ????????HelloWorld?client?=?(HelloWorld)factory.getBean("client");??
  27. ????????User?user1?=?new?User();??
  28. ????????user1.setName("马克思");??
  29. ????????user1.setDescription("怀念马克思");??
  30. ????????User?user2?=?new?User();??
  31. ????????user2.setName("恩格斯");??
  32. ????????user2.setDescription("怀念恩格斯");??
  33. ????????List<User>?userList=?new?ArrayList<User>();??
  34. ????????userList.add(user1);??
  35. ????????userList.add(user2);??
  36. ????????String[]?res?=?client.SayHiToUserList(userList);??
  37. ????????System.out.println(res[0]);??
  38. ????????System.out.println(res[1]);????
  39. ??????????
  40. ????}??
  41. ??
  42. }??


?

4、? 启动tamcat发布webService

然后在浏览器输入地址:http://localhost:8080/cxf/webservice/helloWorld?wsdl

如图:20140805133642.jpg


说明webService服务发布成功。

?

5、? 运行客户端代码访问webService

右键 run as 选择java application,控制台打印如图:

20140805134838.jpg


Ok,客户端访问也成功了。

?此篇实现了webService服务的发布以及在本工程下的客户端调用服务的示例,或许不是很直观。

请看下一篇:CXF客户端代码生成与服务调用(二)

http://blog.csdn.net/hu_shengyang/article/details/38384839

本文参照了:使用 CXF webservice简单例子

http://www.cnblogs.com/frankliiu-java/articles/1641949.html

(编辑:李大同)

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

    推荐文章
      热点阅读