springmvc集成cxf的方法


最近需要在項目中增加webservice接口,供三方調用,下面就把集成的方法展示如下,供大家參考:

第一步:服務端的發布;

1:配置web.xml文件,添加cxf的servlet

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

2:maven導入需要的cxf jar包

<properties>
  <cxf.version>3.2.1</cxf.version>
</properties>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxws</artifactId>
  <version>${cxf.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-transports-http</artifactId>
  <version>${cxf.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-core</artifactId>
  <version>${cxf.version}</version>
</dependency>

如果不是maven項目,一般的項目的話,就下載相應的jar包並導入項目中。

3.導入wsdljar包

<dependency>
   <groupId>soap.public</groupId>
   <artifactId>wsdl4j</artifactId>
   <version>1.6.3</version>
   <scope>system</scope>
   <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/wsdl4j-1.6.3.jar</systemPath>
</dependency>

wsdl4j-1.6.3.jar 包放在了工程的lib目錄下,然后引用了本地lib中的此jar包,此處也可以直接從maven倉庫中引用。

4,在增加如下的配置文件,我的命名為cxf-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:context="http://www.springframework.org/schema/context"
       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
    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 引入CXF配置文件,低版本還需引入其他兩個文件 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    
    <jaxws:endpoint id="ssWebService" address="/ssWebService"
        implementor="com.webservice.service.impl.SsWebServiceImpl" />
</beans>

然后在spring配置文件中,導入cxf-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:soap="http://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://cxf.apache.org/bindings/soap 
    http://cxf.apache.org/schemas/configuration/soap.xsd 
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd">

    <context:component-scan base-package="com.webservice.*"/>
    
    <!-- 事務管理器 -->
     <bean id="transactionManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--         數據源 -->
        <property name="dataSource" ref="dataSource" />
    </bean> 
    <!-- 通知 -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
<!--             傳播行為 -->
            <tx:method name="import*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice> 
    <!-- 切面 -->
     <aop:config>
         <aop:pointcut id="operation" expression="execution(* com.webservice.*.service.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="operation" />
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.webservice.*.service*.*.*(..))" />
    </aop:config>
    
    
    
    <import resource="classpath:config/cxf-servlet.xml"/> 
     
</beans>

 

最后建立webservice服務端的代碼

接口類和實現類代碼

package com.webservice.service;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import com.webservice.data.pojo.ExcelUser;

@WebService
@SOAPBinding(style = Style.RPC)
public interface SsWebService {
    
    public ExcelUser getUser(@WebParam(name="loginName") String loginName,@WebParam(name="password") String password);
    
    
    
}
package com.webservice.service.impl;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.webservice.data.pojo.ExcelUser;
import com.webservice.data.service.LoginService;
import com.webservice.service.SsWebService;

@Transactional
@Service
@WebService(endpointInterface = "com.webservice.service.SsWebService", serviceName = "SsWebService")
public class SsWebServiceImpl implements SsWebService {
    
    @Autowired
    private LoginService loginService;

    @Override
    public ExcelUser getUser(String loginName, String password) {
        
        return loginService.findUserByNameAndPwd(loginName, password);
    }

    

}

到此服務端已建立完成,訪問地址:http://127.0.0.1:8080/工程名/webService/ssWebService?wsdl即可看到發布的webService接口內容。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM