zoukankan      html  css  js  c++  java
  • 使用CXF做webservice整合现有项目的例子

      从网上看了很多CXF的资料,大部分都是单独的作为一个webservice项目,对于在现有的spring项目上提供webservice服务的例子基本没有找到。

         我做的这个例子是介绍怎么把cxf整合到现有的spring项目中,现在只做到可以传简单的字符串和JAVABEAN,复杂的以后研究。

          这是例子的下载地址:一个简单的CXF例子

         一,应用cxf应该先把该服务所需要的架包加载进项目中。

                 对于一个已经搭建好的spring项目,我做的项目中所缺少的架包是

                  cxf-2.4.3.jar   ,   neethi-3.0.1.jar     ,     wsdl4j-1.6.2.jar     ,   xmlschema-core-2.0.1.jar    ,commons-logging-1.1.1.jar   ,spring一系列的架包

        二,首先是服务接口

    [java] view plain copy
     
    1. package com.zcz.cxf.service;  
    2.   
    3.   
    4.   
    5. import javax.jws.WebService;  
    6.   
    7.   
    8. @WebService   
    9. public interface GreetingService {   
    10.    public String greeting(String userName);  
    11.    public String say(String eat);  
    12.    //public String user(User user);  
    13. }   

          三,编写服务实现类

    [java] view plain copy
     
    1. package com.zcz.cxf.service.impl;  
    2.   
    3. import javax.jws.WebService;  
    4.   
    5. import com.zcz.cxf.service.GreetingService;  
    6.   
    7. @WebService  
    8. public class GreetingServiceImpl implements GreetingService {   
    9.   
    10.        public String greeting(String userName){  
    11.              return "你好! " + userName;   
    12.        }  
    13.   
    14.       public String say(String eat) {  
    15.           return "该吃饭了"+eat;  
    16.     }  
    17.   
    18.       
    19. }   


     

           四,配置spring启动时,加载的xml文件,按照下面的xml在原有的基础上进行添加编辑,只添加我标注的红色部分即可

    [html] view plain copy
     
    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 http://cxf.apache.org/schemas/jaxws.xsd">  
    9.    
    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.       
    14.     <jaxws:endpoint id="greetingService"  
    15.         implementor="com.gary.test.ws.service.impl.GreetingServiceImpl"   
    16.         address="/GreetingService" />  
    17. </beans>   

           xmlns:jaxws=http://cxf.apache.org/jaxws

           http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd

          <import resource="classpath:META-INF/cxf/cxf.xml" />

          <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

          <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

           <jaxws:endpoint id="greetingService"implementor="com.gary.test.ws.service.impl.GreetingServiceImpl" address="/GreetingService" />

           五,配置web.xml对于webservice的调用,在web.xml中添加以下代码即可

    [html] view plain copy
     
    1. <servlet>  
    2.         <servlet-name>CXFServlet</servlet-name>  
    3.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    4.         <load-on-startup>1</load-on-startup>  
    5.     </servlet>  
    6.     <servlet-mapping>  
    7.         <servlet-name>CXFServlet</servlet-name>  
    8.         <url-pattern>/cxf/*</url-pattern>  
    9.     </servlet-mapping>  
    [html] view plain copy
     
    1.   


           六,启动项目如果访问http://localhost:8080/springmvcModel/cxf,出现如下图所示的内容则表示基于cxf的webservice配置成功



          七,客户端对于该接口的调用

                  首先,新建一个与服务器端相同的服务接口类GreetingService

                  其次,写调用服务的类

    [java] view plain copy
     
    1. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
    2.   
    3.   
    4. public class TestGreetingService {  
    5.   
    6.     public static void main(String[] args) {  
    7.         //创建WebService客户端代理工厂    
    8.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    
    9.         //注册WebService接口    
    10.         factory.setServiceClass(GreetingService.class);    
    11.         //设置WebService地址    
    12.         factory.setAddress("http://localhost:8080/springmvcModel/cxf/GreetingService");    
    13.         GreetingService greetingService = (GreetingService)factory.create();    
    14.         System.out.println("开始调用webservice...");    
    15.         System.out.println("返回的信息是:"+greetingService.say("米饭"));   
    16.   
    17.     }  
    18.   
    19. }  

          配置成功微笑,做完之后发现其实很简单,虽然不明白原理,但是会做基本的应用。

  • 相关阅读:
    Notes about "Exploring Expect"
    Reuse Sonar Checkstyle Violation Report for Custom Data Analysis
    Eclipse带参数调试的方法
    MIT Scheme Development on Ubuntu
    Manage Historical Snapshots in Sonarqube
    U盘自动弹出脚本
    hg的常用配置
    Java程序员的推荐阅读书籍
    使用shared memory 计算矩阵乘法 (其实并没有加速多少)
    CUDA 笔记
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/7586841.html
Copyright © 2011-2022 走看看