spring+mybatis+cxf整合

一、项目说明

项目环境:jdk1.7+tomcat7+idea2018+cxf3.1.8+maven+mysql5.1.40

源代码github地址:https://github.com/tmAlj/smc

实现目标:通过cxf实现服务的发布与调用

二、整合步骤(默认除cxf以外其他配置已整合)

(1)pom.xml中添加cxf依赖(下载地址:http://mvnrepository.com/

<!-- cxf依赖start -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.8</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.8</version>
</dependency>
<!-- cxf依赖end -->

(2)web.xml中添加cxf映射

<!-- cxf配置-->
<servlet>
    <description>CXF Endpoint</description>
    <display-name>cxf</display-name>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/cxfs/*</url-pattern>
</servlet-mapping>

(3)web.xml中上下文扫描中添加cxf-config.xml

<!-- 定义上下文,扫描spring配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:spring-config.xml,
        classpath:cxf-config.xml
    </param-value>
</context-param>
<listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener
         </listener-class>
 </listener>

(4)新建cxf配置文件cxf-config.xml

        注:implementor的地址为接口实现类的路径

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

    <!--cxf服务配置-->
    <jaxws:endpoint id="helloWebservice" implementor="com.wsd.cxf.server.CxfServices" address="/services"/>

</beans>

(5)新建cxf服务端接口

package com.wsd.cxf.server;

import javax.jws.WebService;

/**
 * Created by tm on 2018/8/2.
 * 服务接口类
 */
@WebService
public interface CxfImpls {
    /**
     * 不带参数测试服务
     */
    public String sayHello();

    /**
     * 带参数测试服务
     * @param param
     */
    public String sayHellos(String param);

    /**
     * 查询数据库测试服务
     * @param param
     */
    public String getUser();
}

(6)新建服务端接口实现类

        注:endpointInterface的路径为接口类的路径

package com.wsd.cxf.server;

import com.wsd.model.User;
import com.wsd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jws.WebService;

/**
 * Created by tm on 2018/8/2.
 * 服务实现类
 */
@WebService(endpointInterface = "com.wsd.cxf.server.CxfImpls")
public class CxfServices implements CxfImpls {
    @Autowired
    UserService us;
    /**
     * 不带参数测试方法
     */
    public String sayHello() {
        System.out.println("say hello cxf");
        return "cxf";
    }

    /**
     * 带参数测试方法
     * @param param
     */
    public String sayHellos(String param) {
        System.out.println("say hello cxf=>" +  param);
        return "cxf";
    }

    /**
     * 查询数据库测试方法
     * @return
     */
    public String getUser() {
        User user = us.getUser();
        System.out.println("userName=>" +  user.getName());
        return null;
    }
}

(7)服务端发布成功示例

注:访问http://localhost:8081/smc/cxfs/services?wsdl出现下图即发布成功,8081和smc为tomcat配置,cxfs为web.xml中配置的servlet,services为cxf-config.xml中配置的address

bda6e6c130e2e88f53b3753e3b51b41670a.jpg

(8)新建客户端调用测试类

        注:客户端的cxf版本要与服务端的匹配,如有返回值,返回为一个数组

package com.wsd.cxf.client;

import com.wsd.service.UserService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by tm on 2018/8/2.
 * cxf客户端类
 */
@Component("cxfClient")
public class CxfClient {
    public static void main(String[] orgs){

        String url = "http://localhost:8081/smc/cxfs/services?wsdl";

        /* 不带参数测试方法
        String method = "sayHello";
        callWebService(url, method, new Object[]{});
        */

        /* 带参数测试方法
        String param = "jack";
        String method = "sayHellos";
        callWebService(url, method, new Object[]{param});
        */

        /* 查询数据库测试方法*/
        String method = "getUser";
        callWebService(url, method, new Object[]{});
    }
    /**
     * 调用webservice通用方法
     * @param url 服务端wsdl地址
     * @param method 服务端配置的接口实现类地址
     * @param data 服务端方法参数
     */
    public static void callWebService(String url,String method,Object...data){
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        Object[] results = null;
        try {
            results = client.invoke(method, data);
            System.out.println("results:"+results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

转载于:https://my.oschina.net/tij/blog/1922193

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值