• 学习webservice之cxf(8):Spring整合CXF


    新建一个maven项目,添加依赖

    <!-- 添加Spring支持 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-tx</artifactId>
             <version>4.1.7.RELEASE</version>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
         
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
         
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
         
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
         
         
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
         
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        
        <!-- 添加cxf支持  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.5</version>
        </dependency>
         
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.5</version>
        </dependency>
         
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.5</version>
        </dependency>

    再添加一个application.xml文件在resource文件夹下

    <?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:p="http://www.springframework.org/schema/p"  
        xmlns:aop="http://www.springframework.org/schema/aop"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:jee="http://www.springframework.org/schema/jee"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xsi:schemaLocation="    
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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-servlet.xml"/>
        
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.rg2.webservice" />
        
        <!-- 定义服务提供者  -->
        <jaxws:endpoint
            implementor="#helloWorld"
            address="/HelloWorld"
         ></jaxws:endpoint>
    </beans>

    再在web.xml文件下加上相关配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>WS_Spring_CXF</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- Spring配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
         
        <!-- Spring监听器 -->
        <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>  
        </servlet>  
         
        <servlet-mapping>  
           <servlet-name>CXFServlet</servlet-name>  
           <url-pattern>/webservice/*</url-pattern>  
        </servlet-mapping>
    </web-app>

    最后将之前所做的

    拷贝到java包下即可

     如果配置cxf拦截器的话

    修改application.xml文件,将定义服务提供者修改如下

    <!-- 定义服务提供者  -->
        <jaxws:endpoint
            implementor="#helloWorld"
            address="/HelloWorld">
            <!-- 添加in拦截器 -->
            <jaxws:inInterceptors>
                <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
                <bean class="com.rg2.intercepter.MyIntercepter"></bean>
            </jaxws:inInterceptors>
            <!-- 添加out拦截器 -->
            <jaxws:inInterceptors>
                <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
         </jaxws:endpoint>

     访问的话重新用wsdl2生成,Client.java会报错,只要将错误的根据提示略微修改即可

  • 相关阅读:
    接口测试基础-ddt
    接口测试基础-json.loads-json数据格式转换成字典格式
    接口测试基础-拆包
    接口测试基础-导入第三方模块和删除第三方模块
    接口测试基础-正则表达式和参数化
    接口测试基础-Exece的操作-openpyxl模块
    matlab学习
    matlab学习笔记
    SSH框架配置及Maven使用
    运筹学框架图
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/9278409.html
Copyright © 2020-2023  润新知