springmvc集成cxf的方法

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

第一步:服务端的发布;

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

    
        cxf
        class>org.apache.cxf.transport.servlet.CXFServletclass>
        1
    
 
    
        cxf
        /webService/*
    

2:maven导入需要的cxf jar包


  3.2.1


  org.apache.cxf
  cxf-rt-frontend-jaxws
  ${cxf.version}


  org.apache.cxf
  cxf-rt-transports-http
  ${cxf.version}


  org.apache.cxf
  cxf-core
  ${cxf.version}

如果不是maven项目,一般的项目的话,就下载相应的jar包并导入项目中。

3.导入wsdljar包


   soap.public
   wsdl4j
   1.6.3
   system
   ${project.basedir}/src/main/webapp/WEB-INF/lib/wsdl4j-1.6.3.jar

wsdl4j-1.6.3.jar 包放在了工程的lib目录下,然后引用了本地lib中的此jar包,此处也可以直接从maven仓库中引用。

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

"1.0" encoding="UTF-8"?>

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


    
    "classpath:META-INF/cxf/cxf.xml" /> 
    
    "ssWebService" address="/ssWebService"
        implementor="com.webservice.service.impl.SsWebServiceImpl" />

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

"1.0" encoding="UTF-8"?>
"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">

    base-package="com.webservice.*"/>
    
    
     "transactionManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        "dataSource" ref="dataSource" />
     
    
     "txAdvice" transaction-manager="transactionManager">
        

            "import*" propagation="REQUIRED" />
            "insert*" propagation="REQUIRED" />
            "delete*" propagation="REQUIRED" />
            "update*" propagation="REQUIRED" />
            "find*" propagation="SUPPORTS" read-only="true" />
            "get*" propagation="SUPPORTS" read-only="true" />
        
     
    
     
         "operation" expression="execution(* com.webservice.*.service.*.*(..))" />
            ref="txAdvice" pointcut-ref="operation" />
        ref="txAdvice"
            pointcut="execution(* com.webservice.*.service*.*.*(..))" />
    
    
    
    
    "classpath:config/cxf-servlet.xml"/> 
     

 

最后建立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接口内容。

你可能感兴趣的:(springmvc集成cxf的方法)