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

WebService系列博客{十}[CXF简单案例实现]

发布时间:2020-12-16 22:44:41 所属栏目:安全 来源:网络整理
导读:概述: CXF是Apache的一个Web Service框架。搭配Jax-Ws使用将部署好的web service发布到tomcat容器中 简单案例: 1、?首先准备好Jar包。可以到apache的官网下载 2、?将下载好的Jar包一次性导入到项目目录[WEB-INF/lib]文件夹下面 3、?解压lib里面的cxf.jar文件

概述:

CXF是Apache的一个Web Service框架。搭配Jax-Ws使用将部署好的web service发布到tomcat容器中


简单案例:

1、?首先准备好Jar包。可以到apache的官网下载

2、?将下载好的Jar包一次性导入到项目目录[WEB-INF/lib]文件夹下面

3、?解压lib里面的cxf.jar文件,将解压目录下面的[META-INF/cxf]copy到项目的META-INF下面

4、?编写web service接口

[java]? view plain copy
  1. @WebService(name?=?"CxfTester",?targetNamespace?=?"http://org.cxf.com/")??
  2. public?interface?CxfTester?{??
  3. ????public?String?sendMessage(String?sendTo,String?message);??
  4. }??

5、? 编写服务接口实现类

copy
    @WebService(endpointInterface?=?"com.cxf.org.CxfTester")??
  1. class?CxfTesterImpl?implements?CxfTester?{??
  2. ????@Override??
  3. ???? ????????String?str?=?"发送到:"?+?sendTo?+?"信息详情:"?+?Message;??
  4. ????????return?str;??
  5. ????}??
  6. 6、 ?在WEB-INF/建立一个文件夹名为classes。并且新建一个xml文件、名为application

    Context.xml,代码如下:

    [html]? copy
      <?xml?version="1.0"?encoding="UTF-8"?>??
    1. <beans?xmlns="http://www.springframework.org/schema/beans"??
    2. ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
    3. ???????xmlns:jaxws="http://cxf.apache.org/jaxws"??
    4. ???????xsi:schemaLocation="??
    5. ???????????????????????http://www.springframework.org/schema/beans??
    6. ???????????????????????http://www.springframework.org/schema/beans/spring-beans.xsd??
    7. ???????????????????????http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd">??
    8. ?????????
    9. <!--?导入cxf.jar解压出来的文件?-->??????????
    10. import?resource="classpath:META-INF/cxf/cxf.xml"/>??
    11. import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>??
    12. import?resource="classpath:META-INF/cxf/cxf-servlet.xml" ??
    13. <!--?声明Endpoint,???
    14. address?为访问地址的部分地址??
    15. implementor???为实现类??
    16. ?--jaxws:endpoint?id="CxFTester"???
    17. ????address="/CxfTester"???
    18. ????implementor="com.cxf.org.CxfTesterImpl" ??
    19. ??????
    20. <!--?指定客户端访问相关情况??
    21. class???服务端接口??
    22. factory-bean????引用下面声明的类??
    23. factory-method???客户端生成调用对象所用方法??
    24. --bean?id="client"???
    25. ????????class="com.cxf.org.CxfTester"??
    26. ????????????factory-bean="clientFactory"??
    27. ????????????????factory-method="create"></bean ??????????????????
    28. ??????????????
    29. <!--?配置CXF服务代理bean??
    30. serviceClass?:?服务端接口??
    31. address?:?访问地址jaxws:endpoint声明的address在项目名的后面,此处的路径和web.xml配置的urlpattern有联系??
    32. ?-->???????????????
    33. bean?id="clientFactory"?class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" ????property?name="serviceClass"?value="com.cxf.org.CxfTester"property>??
    34. ????property?name="address"?value="http://192.168.1.104:8080/Apache_CXF_1/CxfTester">??

    7、? 编辑web.xml,设置容器启动加载applicationContext.xml文件。并且配置CXFServlet

    copy
      web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
    1. ????xmlns="http://java.sun.com/xml/ns/javaee"???
    2. ????xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"???
    3. ????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee???
    4. ????????????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"???
    5. ????????????id="WebApp_ID"?version="2.5" ??????????????
    6. ??display-name>Apache_CXF ??welcome-file-listwelcome-file>index.html>index.htm>index.jsp>default.html>default.htm>default.jsp ????
    7. ????
    8. ??<!--?配置启动加载项目?-->??
    9. context-param ????????param-name>contextConfigLocation ????????param-value>WEB-INF/classes/applicationContext.xml<!--?配置监听?-->??
    10. listenerlistener-class ????????org.springframework.web.context.ContextLoaderListener??
    11. ??<!--?部署servlet?-->??
    12. servletservlet-name>CXFServletservlet-class ????????org.apache.cxf.transport.servlet.CXFServlet??
    13. load-on-startup>1servlet-mappingurl-pattern>/*web-app 8、? 发布项目、启动tomcat并且访问wsdl文件

      http://localhost:8080/Apache_CXF_1/CxfTester?wsdl


      源码下载地址:

      http://pan.baidu.com/share/link?shareid=216475&uk=1997312776

      (编辑:李大同)

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

    推荐文章
      热点阅读