Web Service笔记(四):利用CXF结合Spring开发web service(转别人的)

一、前言准备:
1、Web Service笔记(二):利用CXF开发Web Service
2、需要的 jar包:需要spring的 jar 包,去掉 jetty 的包。

二、CXF 结合Spring 发布Web Service

1、配置web.xml文件。需要配置spring 与 CXF。
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  <!-- 加载Spring容器配置  start--> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<!-- 设置Spring容器加载配置文件路径 --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:applicationContext-server.xml</param-value> 
</context-param> 
  
<listener> 
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 
</listener> 
 
<!-- 加载Spring容器配置  end-->  
 
<!--    配置CXF start  --> 
<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>/*</url-pattern> 
</servlet-mapping> 
<!--    配置CXF end  --> 


2、配置 spring 的配置文件,增加 CXF 的schema 与配置依赖。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
 
http://cxf.apache.org/jaxws  
http://cxf.apache.org/schemas/jaxws.xsd 
http://cxf.apache.org/jaxrs 
http://cxf.apache.org/schemas/jaxrs.xsd " 

[html] view plain copy 在CODE上查看代码片派生到我的代码片
   <!-- CXF必须引入配置依赖 --> 
   <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"/> 

3、spring结合 CXF 的配置
1)endpoint 标签,也可以使用 server(略)。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<jaxws:endpoint id="" implementor="" address=""> 
</jaxws:endpoint> 

2)address 地址,省略工程名等。如访问地址:http://localhost:8080/Java_WS_Server/helloworld1?wsdl
3)implementor 指定 WS 的服务提供者。支持两种形式:
    ①、直接给定服务提供者的类名。
    ②、设置容器中的一个bean。(推荐)

4)第一种 implementor 方法的配置。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!-- 直接给定服务提供者的类名,不推荐 --> 
<jaxws:endpoint id="helloWorld1" implementor="com.ws.impl.HelloWorldImpl" address="/helloworld"> 
</jaxws:endpoint> 

5)第二种 implementor 方法的配置,引用bean。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!-- 设置容器中的一个bean,用# 引用。推荐。 -->    
<bean id="helloWorld2" class="com.ws.impl.HelloWorldImpl"></bean>  
<jaxws:endpoint id="helloWorldWebService" implementor="#helloWorld2" address="/soap/helloWorld"> 
lt;/jaxws:endpoint> 

5、添加拦截器
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!-- 配置CXF的拦截器 --> 
<bean id="helloWorld3" class="com.ws.impl.HelloWorldImpl"></bean>  
     <jaxws:endpoint id="helloWorldServer" implementor="#helloWorld3" address="/intecepter/helloWorld"> 
    <jaxws:inInterceptors> 
    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 
        <bean class="com.intecepter.MyIntecepter"></bean>            
    </jaxws:inInterceptors> 
     
    <jaxws:outInterceptors> 
    </jaxws:outInterceptors> 
</jaxws:endpoint> 

6、完整的spring的配置文件的代码如下:applicationContext-server.xml
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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://cxf.apache.org/jaxrs 
    http://cxf.apache.org/schemas/jaxrs.xsd ">  
     
    <!-- CXF必须引入配置依赖 --> 
    <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"/> 
     
    <context:property-placeholder /> 
    <context:annotation-config /> 
    <!-- 设置需要进行Spring注解扫描的类包 -->   
    <context:component-scan base-package="com/service" /> 
     
     
    <!--发布方式一: 使用 server --> 
    <!-- 
         注意下面的address,这里的address的名称就是访问的WebService的name      
        <jaxws:server id="userService" serviceClass="com.hoo.service.IComplexUserService" address="/Users"> 
            <jaxws:serviceBean> 
                 要暴露的 bean 的引用  
                <ref bean="userServiceBean"/> 
            </jaxws:serviceBean> 
        </jaxws:server>  
    --> 
     
    <!--发布方式二:使用 endpoint : 
    1、implementor 指定 WS 的服务提供者。支持两种形式: 
        1)直接给定服务提供者的类名。 
        2)设置容器中的一个bean。推荐。 
    2、address :地址,省略工程名等。访问地址:http://localhost:8080/Java_WS_Server/helloworld1?wsdl 
    --> 
     
    <!-- 直接给定服务提供者的类名,不推荐 --> 
    <jaxws:endpoint id="helloWorld1" implementor="com.ws.impl.HelloWorldImpl" address="/helloworld"> 
    </jaxws:endpoint> 
     
    <!-- 设置容器中的一个bean,用# 引用。推荐。 -->    
     <bean id="helloWorld2" class="com.ws.impl.HelloWorldImpl"></bean>  
     <jaxws:endpoint id="helloWorldWebService" implementor="#helloWorld2" address="/soap/helloWorld"> 
     </jaxws:endpoint> 
      
      
     <!-- 配置CXF的拦截器 --> 
     <bean id="helloWorld3" class="com.ws.impl.HelloWorldImpl"></bean>  
     <jaxws:endpoint id="helloWorldServer" implementor="#helloWorld3" address="/intecepter/helloWorld"> 
        <jaxws:inInterceptors> 
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 
            <bean class="com.intecepter.MyIntecepter"></bean>            
        </jaxws:inInterceptors> 
         
        <jaxws:outInterceptors> 
        </jaxws:outInterceptors> 
     </jaxws:endpoint> 
 
     
    <!-- REST WebService 接口--><!-- 
    <jaxrs:server id="restServiceContainer" address="/rest"> 
         暴露的Bean  
        <jaxrs:serviceBeans> 
            <bean class="com.hoo.rest.RESTSampleSource"></bean>  
            <bean class="com.hoo.rest.SurpolicyEntrence"></bean>     
        </jaxrs:serviceBeans> 
        <jaxrs:extensionMappings> 
            <entry key="json" value="application/json" /> 
            <entry key="xml" value="application/xml" /> 
        </jaxrs:extensionMappings> 
        <jaxrs:languageMappings> 
               <entry key="en" value="en-gb"/>   
        </jaxrs:languageMappings> 
    </jaxrs:server> 
     
     
     
     
--></beans> 

7、部署工程,访问 http://localhost:8080/Java_WS_Server,显示如下:


二、CXF 结合Spring 客户端
1、jar包的引进与服务端一致,使用 wsdl2java http://localhost:8080/Java_WS_Server/soap/helloWorld?wsdl 创建客户端。
2、web配置:客户端的web配置可以省略CXF的配置,只需要spring即可。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  <!-- 加载Spring容器配置  start--> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<!-- 设置Spring容器加载配置文件路径 --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:applicationContext-client.xml</param-value> 
</context-param> 
  
<listener> 
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 
</listener> 


3、利用 jaxws:client 标签实现 CXF 的客户端的spring结合。记得引入CXF的 schema 规范与本身的配置。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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://cxf.apache.org/jaxrs 
    http://cxf.apache.org/schemas/jaxrs.xsd ">  
     
    <!-- CXF必须引入配置依赖 --> 
    <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"/> 
     
    <context:property-placeholder /> 
    <context:annotation-config /> 
    <!-- 设置需要进行Spring注解扫描的类包 -->   
    <context:component-scan base-package="com" /> 
     
     
    <!-- 配置代理 : address: 配置为服务端的地址 id:与客户端注入的保持一致 --> 
    <jaxws:client id="helloWorld" serviceClass="com.ws.HelloWorld"  
             address="http://localhost:8080/Java_WS_Server/soap/helloWorld" > 
              
    </jaxws:client> 
     
    </beans> 

4、代码调用
[java] view plain copy 在CODE上查看代码片派生到我的代码片
/**
* 访问 server
*/ 
private void visitSpringServer() { 
 
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");//获取配置文件 
    HelloWorld service = ctx.getBean("helloWorld", HelloWorld.class);//获取bean 
     
    System.out.println(service.sayHi("邵小宝"));//调用服务端 
     





原博客地址: http://blog.csdn.net/u012228718/article/details/41381595

猜你喜欢

转载自lfc-jack.iteye.com/blog/2365987