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中定义

   

            <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

            

               <!-- cxf配置 -->
     <import resource="classpath*:META-INF/cxf.xml"/>
     <import resource="classpath*:META-INF/cxf-extension-soap.xml"/>
     <import resource="classpath*:META-INF/cxf-servlet.xml"/>
     <bean id="greetingServiceBean" class="com.ws.GreetingImpl"></bean>
    
     <jaxws:endpoint id="greetingService" implementor="#greetingServiceBean" address="/greeting" />

</beans>


四.修改web.xml

    <servlet>
      <servlet-name>CXFService</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>CXFService</servlet-name>
      <url-pattern>/cxf/*</url-pattern>
  </servlet-mapping>


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

    出现:

     该 XML 文件并未包含任何关联的样式信息。文档树显示如下。
      <wsdl:definitions name="GreetingImplService" targetNamespace="http://ws.com/"><wsdl:message name="toSay"><wsdl:part name="userName" type="xsd:string">
    </wsdl:part></wsdl:message><wsdl:message name="toSayResponse">
  </wsdl:message><wsdl:portType name="GreetingI"><wsdl:operation name="toSay"><wsdl:input message="tns:toSay" name="toSay">
    </wsdl:input><wsdl:output message="tns:toSayResponse" name="toSayResponse">
    </wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="GreetingImplServiceSoapBinding" type="tns:GreetingI"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="toSay"><soap:operation soapAction="" style="rpc"/><wsdl:input name="toSay"><soap:body namespace="http://ws.com/" use="literal"/></wsdl:input><wsdl:output name="toSayResponse"><soap:body namespace="http://ws.com/" use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="GreetingImplService"><wsdl:port binding="tns:GreetingImplServiceSoapBinding" name="GreetingImplPort"><soap:address location="http://localhost:8088/HibernateAnnotation/cxf/greeting"/></wsdl:port></wsdl:service></wsdl:definitions>


说明服务发布成功!



创建一个客户端来调用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

            <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    
    
     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" p:serviceClass="com.ws.GreetingI"
     p:address="http://localhost:8088/HibernateAnnotation/cxf/greeting"
     />
    
     <bean id="client" factory-bean="clientFactory" factory-method="create"/>
         
    
</beans>

    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的调用也结束了





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值