CXF与Spring的集成demo

一.准备工作

    下载apache-cxf应用包,我目前使用的是apache-cxf-2.7.5

二.定义服务接口

    package com.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 *
 * @author guoming
 *
 */
@WebService
@SOAPBinding(style=Style.RPC)
public interface GreetingI {

    @WebMethod(operationName="toSay")
    public void say(@WebParam(name="userName")String userName);
}

---------------------------------------------------------------------------------------------------------------------------------------

package com.ws;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 *
 * @author guoming
 *
 */
@WebService(endpointInterface="com.ws.GreetingI")
@SOAPBinding(style=Style.RPC)
public class GreetingImpl implements GreetingI {

    public GreetingImpl(){}
    @Override
    public void say(String userName) {
        // TODO Auto-generated method stub
        System.out.println("Hi:"+userName);
    }

}


三.在spring上下文中application.xml中定义

   

           
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://cxf.apache.org/jaxws
                           http://cxf.apache.org/schemas/jaxws.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

            

               
    
    
    
    
    
    



四.修改web.xml

   
      CXFService
      org.apache.cxf.transport.servlet.CXFServlet
 

 
      CXFService
      /cxf/*
 


五.启动tomcat,访问 http://localhost:8088/HibernateAnnotation/cxf/greeting?wsdl             

    出现:

     该 XML 文件并未包含任何关联的样式信息。文档树显示如下。
     
   

 

   

   


说明服务发布成功!



创建一个客户端来调用webservice


      1.新建一个java project,并加入cxf相应的包

      2.将webservice的接口类导出成jar包,也添加到Build Path,主要目的是客户端要用到服务端的GreetingI这个接口。如果不想导入这个jar包也可以,只要在客户端创建一个一摸一样的接口类:GreetingI,特别要注意以下两点:
    1)接口前面要添加@Webservice的标记,不然会抛出一个 javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHelloWorld.
    2)包路径也要一样,不然会抛出一个ClassCastException: $Proxy29 cannot be cast to...

      3.配置spring-client.xml

           
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://cxf.apache.org/jaxws
                           http://cxf.apache.org/schemas/jaxws.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    
          p:address="http://localhost:8088/HibernateAnnotation/cxf/greeting"
     />
    
    
         
    

    4.测试

        public static void main(String[] args) {
        
        ApplicationContext ctx=new ClassPathXmlApplicationContext("basic.xml");
        com.ws.GreetingI greet=null;
        try {
            greet = (com.ws.GreetingI)ctx.getBean("client");
        } catch (Exception e) {
            System.out.println("error...........");
            e.printStackTrace();
        }
        if (null != greet) {
            
            
            String result=greet.say("lisi");
            System.out.println(result);
        }
       
     输出:Hi:lisi


 测试通过,至此一个webservice的调用也结束了





你可能感兴趣的:(CXF与Spring的集成demo)