且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

spring和CXF集成来实现webservices

更新时间:2022-08-21 18:27:49

最近在负责一个大系统的实施,经过需求分析之后,将系统分为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
 新建web project ,并加入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 

<?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: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">

    <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" /> 

 

    <bean id="hello" class="test.HelloWorldImpl" /> 

    <jaxws:endpoint id="helloWorld" implementor="#hello" 

        address="/HelloWorld" /> 

  </beans>

注意:<jaxws:endpoint id="helloWorld" implementor="#hello" 

        address="/HelloWorld" /> 

id:指在spring配置的beanID.

Implementor:指明具体的实现类.

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

3. 配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="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

<beans xmlns="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服务的接口一致-->

    <bean id="client" class="test.HelloWorld"

      factory-bean="clientFactory" factory-method="create"/>

   

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
     <!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->

     <property name="serviceClass" value="test.HelloWorld"/>
     <!-- 这个address一定要注意,正确的-->

     <property name="address" value="http://localhost:8080/<web-app-name>/HelloWorld"/>

    </bean>     

</beans>

4.测试:

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import test.HelloWorld;

 

public class Test {  

     

    public static void main(String[] args) {  

 

        ApplicationContext ctx = new ClassPathXmlApplicationContext(  

                "spring-client.xml");  

        HelloWorld client = (HelloWorld) ctx.getBean("client");  

        String result = client.sayHello("Glen!");  

        System.out.println(result);  

    }  

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