【WebService】springboot整合cxf实现webService远程调用

一、前言

        在做接口对接的时候需要接口以HTTPS和WebService的形式暴露,虽然最近这几年restful非常火爆,但在一些特定的领域或者一些老旧系统中仍然采用WebService的形式来实现远程通信。

       上一篇博客讲了WebService实现远程调用,在博文的最后,自己阐述了关于那种客户端调用存在的问题,如大家有疑问,欢迎留言区指正。

二、实现

       废话上一篇博客已经说得差不多了。直接上代码吧。

1、首先pom文件中添加如下依赖

<!-- CXF webservice -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.1.11</version>
		</dependency>
		<!-- CXF webservice -->

2、服务接口

package com.zero.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name="CommonService",//暴露服务名称
        targetNamespace = "http://service.zero.com/") //命名空间,一般是接口的倒叙
public interface CommonService {
    @WebMethod
    @WebResult(name="String",targetNamespace = "")
    public String sayHello(@WebParam(name="userName")String name);

}

3、接口实现

package com.zero.service.impl;

import com.zero.service.CommonService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@WebService(serviceName = "CommonService",//与接口中指定的name一直
        targetNamespace = "http://service.zero.com/",// 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.zero.service.CommonService"//接口地址
)
@Component
public class CommonServiceImpl implements CommonService {
    @Override
    public String sayHello(String name) {
        return "Hello,"+name;
    }
}

4、cxf发布WebService服务

     默认服务在ip:port/services/**路径下面

package com.zero.config;

import com.zero.service.CommonService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {
    @Autowired(required = false)
    private Bus bus;
    @Autowired(required = false)
    CommonService commonService;
    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint=new EndpointImpl(bus,commonService);
        endpoint.publish("/CommonService");

        return endpoint;
    }
}

5、cxf调用WebService接口

package com.zero.service;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class CxfClient {
    public static void main(String[] args) {
        cl2();
    }
    /**
     * 方式1.代理类工厂的方式,需要拿到对方的接口
     */
    public static void cl1() {
        try {
            // 接口地址
            String address = "http://localhost:8089/services/CommonService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
            // 创建一个代理接口实现
            CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String userName = "LemmonTree";
            // 调用代理接口的方法调用并返回结果
            String result = cs.sayHello(userName);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 动态调用方式
     */
    public static void cl2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8089/services/CommonService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,// PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sayHello","LemmonTree");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}

三、总结

       本文并没有使用wsdl文档生成java类,而是采用cxf完成服务端的发布和客户端的调用。

相关文章

今天小编给大家分享的是Springboot下使用Redis管道(pipeline...
本篇文章和大家了解一下springBoot项目常用目录有哪些。有一...
本篇文章和大家了解一下Springboot自带线程池怎么实现。有一...
这篇文章主要介绍了SpringBoot读取yml文件有哪几种方式,具有...
今天小编给大家分享的是SpringBoot配置Controller实现Web请求...
本篇文章和大家了解一下SpringBoot实现PDF添加水印的方法。有...