spring和CXF集成来实现webservices

最近在负责一个大系统的实施,经过需求分析之后,将系统分为5个子系统,我们采用SOA架构,分模块开发。项目组中最大的一个争议就是,子系统之间的通讯问题,大家提出了两种方案:一、如果5个子系统最后发布为5个war包,那么相互之间就不能直接调用,而是需要通过webservices等通讯方式,那会增加一些开发工作量;二、如果5个子系统合并在一个大工程中,下面放所有的模块,那子系统间的访问很简单,但是日常的开发管理会存在比较大的风险。

 

 

 

从管理的角度来看,我是比较偏向第一种方案,因为这样结构更清晰更简单,开发人员之间的相互影响也较小,还有一个好处就是,可以将不同的子系统发布在不同的应用服务器上,避免了由于某一个子系统崩溃导致整个系统的崩溃。于是,我对webservices的开发技术进行了研究,发现用spring和CXF集成来发布webservice和调用webservice都非常的简单,也加大了我选择第一种方案的决心。下面就简单介绍一个spring和CXF集成的示例。

 

一、 准备工作。

        1、下载apache-cxf的应用包,地址:http://cxf.apache.org/download.html,我选择的是2.4.1版本。



二、发布webservices

1新建webproject ,并加入apache-cxf-2.4.1\lib所有包,编写要发布的web service接口和实现.这一步,与前面一样。 

1)创建一个接口类,并加上webservice标记

import javax.jws.WebService;
@WebService 
public interface HelloWorld {  
     public String sayHello(String text);  
}

 


2)创建上面这个接口的实现类

import javax.jws.WebService;  
@WebService(endpointInterface="test.HelloWorld")  
public class HelloWorldImpl implements HelloWorld {  
      public String sayHello(String text) {  
                  return "Hello" + text ;  
    }  
  } 

 

@WebService 注解表示是要发布的web服务,endpointInterface的值是该服务类对应的接口。

2.spring-cxf.xml配置发布的web service 

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
   <importresource="classpath:META-INF/cxf/cxf.xml"/> 
   <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> 
   <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/> 
 
   <beanid="hello"class="test.HelloWorldImpl"/> 
   <jaxws:endpointid="helloWorld"implementor="#hello" 
       address="/HelloWorld"/> 
  </beans >

 

注意:<jaxws:endpointid="helloWorld"implementor="#hello"   address="/HelloWorld"/> 

id:指在spring配置的beanID.

Implementor:指明具体的实现类.

Address:指明这个web service的相对地址,

 

3. 配置web.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?> 
<web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >  
   <context-param >  
       <param-name > contextConfigLocation</param-name >  

                   <!--spring配置文件放在WEB-INF目录下-->
       <param-value > /WEB-INF/spring-cxf.xml</param-value >  
   </context-param >  
   <listener >  
       <listener-class >  
         org.springframework.web.context.ContextLoaderListener 
       </listener-class >  
   </listener >  

         <servlet >  
       <servlet-name > CXFServlet</servlet-name >  
       <servlet-class >  
            org.apache.cxf.transport.servlet.CXFServlet  
       </servlet-class >  
       <load-on-startup > 1</load-on-startup >  
   </servlet >  
   <servlet-mapping >  
       <servlet-name > CXFServlet</servlet-name >  
       <url-pattern > /*</url-pattern >  
   </servlet-mapping >  
</web-app >  

 

4.部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/HelloWorld?wsdl,将显示这个web servicewsdl.

注意:如果web.xml配置<servlet-name > CXFServlet</servlet-name >  

       <url-pattern > /ws/*</url-pattern >  

则访问地址为:http://localhost:8080/<web-app-name>/ws/HelloWorld?wsdl 

到此webservices就发布成功了。

 

 

三、创建一个客户端来调用webservices

 

1、同样新建java project ,并加入apache-cxf-

2.0.7

\lib所有包,添加到Build Path

 

2、将webservice的接口类导出成jar包,也添加到Build Path,主要目的是客户端要用到服务端的HelloWorld这个类。如果不想导入这个jar包也可以,只要在客户端创建一个一摸一样的接口类:HelloWorld,特别要注意以下两点:

 

    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

<beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd" >
      <!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->
   <beanid="client"class="test.HelloWorld"
     factory-bean="clientFactory"factory-method="create"/>
   
   <beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > 

     <!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->
     <propertyname="serviceClass"value="test.HelloWorld"/>

     <!-- 这个address一定要注意,正确的-->
     <propertyname="address"value="http://localhost:8080/<web-app-name>/HelloWorld"/>
    </bean >      
</beans >

 

4.测试:

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
 
importtest.HelloWorld;
 
public class Test {  
     
   public static void main(String[] args) {  
 
        ApplicationContext ctx =newClassPathXmlApplicationContext(  
               "spring-client.xml");  
        HelloWorld client = (HelloWorld) ctx.getBean("client");  
        String result = client.sayHello("Glen!");  
        System.out.println(result);  
    }  
} 

 

结果输出“Hello,Glen!”,测试通过,至此一个webservice的调用也成功了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值