原创

SpringBoot编写WebService客户端示例

温馨提示:
本文最后更新于 2022年09月27日,已超过 589 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

1.创建SpringBoot Maven 项目,JDK1.8

创建地址:https://start.spring.io/

先不引入依赖,直接打包下载

2.项目导入Eclipse,pom文件添加依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
		
<!-- 进行jaxes 服务开发 -->
<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.0.1</version>
</dependency>

3.编写ClientUtil调用WebService工具类

package cn.richwit.webservicekhd.util;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**   * Apache CXF 是开源的WebService框架,CXF帮助您使用前端编程api(如JAX-WS和JAX-RS)构建和开发服务。这些服务可以使用多种协议,如SOAP、XML/HTTP、RESTful HTTP或CORBA,并在多种传输协议(如HTTP、JMS或JBI)上工作* 采用动态工厂方式 不需要指定服务接口
* @author 王小东  
* @date 2022年9月26日  下午4:39:10
* @version 1.0  
*/
public class ClientUtil {
	public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
        return JaxWsDynamicClientFactory.newInstance().createClient(wsdUrl).invoke(operationName, params)[0].toString();
    }
}

4.编写TestController测试方法

package cn.richwit.webservicekhd.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import cn.richwit.webservicekhd.util.ClientUtil;

/**   
* @author 王小东  
* @date 2022年9月26日  下午4:40:33
* @version 1.0  
*/
@Component
public class TestController {
	//在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文。
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    @Scheduled(cron="*/10 * * * * ?")
    public String getMessage()  {
        Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
        System.out.println("----------开始调用webservice接口----------");
        String url = "http://localhost:8080/webService/TagerService?wsdl";
        String methodName = "message";
        System.out.println("Calling::" + url);
        String result="";
        try {
            result=ClientUtil.callWebSV(url, methodName,"");
        } catch (Exception e) {
            System.err.println("接口调用失败!!!!");
            return "失败";
        }
        System.out.println("---------TagerService接口调用成功-------获取到的数据::"+result);
        return result;
    }

    @Scheduled(cron="*/10 * * * * ?")
    public String getMessage2()  {
        Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
        System.out.println("---------开始调用webservice接口---------");
        String url = "http://localhost:8080/webService/TestService?wsdl";
        String methodName = "sendMessage";
        System.out.println("Calling::" + url);
        String result="";
        try {
            result=ClientUtil.callWebSV(url, methodName, "王小东");
        } catch (Exception e) {
            System.err.println("接口调用失败!!!!");
            return  "失败";
        }
        System.out.println("----------TestService接口调用成功------获取到的数据::"+result);
        return result;
    }
}

5.启动项目测试

启动前必须保证服务端已经启动,否则调用失败。

注意项目端口不能冲突,如默认内置tomcat端口8080占用,则需要再application.yml文件中配置更改端口,这里更改成9000

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为80
  port: 9000
  servlet:
    # 应用的访问路径
    context-path: /
  tomcat:
    # tomcat的URI编码
    uri-encoding: UTF-8
    # tomcat最大线程数,默认为200
    max-threads: 800
    # Tomcat启动初始化的线程数,默认值25
    min-spare-threads: 30
 

本项目使用了定时器自动调用,所以只需要启动就可以再控制台看到调用打印结果。

如果定时器未生效,请移步博主文章:SpringBoot的定时器@Scheduled注解使用方法

正文到此结束
该篇文章的评论功能已被站长关闭
本文目录