文章目录

  • 一、服务端代码开发
    • 1、pom依赖
    • 2、接口类
    • 3、接口实现类
    • 4、webservice配置文件
  • 2、客户端开发
    • (1)pom依赖
    • (2)封装客户端方法clientUtil
    • (3)调用接口类
    • (4)运行结果

我们经常需要在两个系统之间进行一些数据的交互,这时候我们就需要开发数据交互接口。

一般来说,遇到比较多的接口有HTTP接口、WebService接口、FTP文件传输。今天我要来学习一下在SpringBoot框架下进行简单的webservice接口的开发。

一、服务端代码开发

创建了两个wbservice接口TestService和CatService。

1、pom依赖

导入相关的依赖包。

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.6</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.6</version></dependency>

2、接口类

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.WebServiceProvider;@WebService(name = "TestService", // 暴露服务名称targetNamespace = "http://server.webservice.Bag.admin.com"// 命名空间,一般是接口的包名倒序
)
public interface TestService {@WebMethodpublic String sendMessage(@WebParam(name = "username") String username);@WebMethodpublic boolean getFlag(@WebParam(name = "username") String username);
}
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService(name = "CatService", // 暴露服务名称targetNamespace = "http://server.webservice.Bag.admin.com"// 命名空间,一般是接口的包名倒序
)
public interface CatService {@WebMethodpublic String message(@WebParam(name = "name") String name);
}

3、接口实现类

import com.admin.Bag.webservice.server.TestService;
import org.springframework.stereotype.Component;import javax.jws.WebService;@WebService(serviceName = "TestService", // 与接口中指定的name一致targetNamespace = "http://server.webservice.Bag.admin.com", // 与接口中的命名空间一致,一般是接口的包名倒endpointInterface = "com.admin.Bag.webservice.server.TestService"// 接口地址
)
@Component
public class TestServiceImpl implements TestService {@Overridepublic String sendMessage(String username) {return "=====Hello! " + username + "=====";}@Overridepublic boolean getFlag(String username) {//return true;}
}
import com.admin.Bag.webservice.server.CatService;
import org.springframework.stereotype.Component;import javax.jws.WebService;@WebService(serviceName = "CatService", // 与接口中指定的name一致targetNamespace = "http://server.webservice.Bag.admin.com", // 与接口中的命名空间一致,一般是接口的包名倒endpointInterface = "com.admin.Bag.webservice.server.CatService"// 接口地址
)
@Component
public class CatServiceImpl implements CatService {@Overridepublic String message(String name) {//return "一只小猫猫";}
}

4、webservice配置文件

import com.admin.Bag.webservice.server.impl.CatServiceImpl;
import com.admin.Bag.webservice.server.impl.TestServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;@Configuration
public class cxfConfig {@Beanpublic ServletRegistrationBean disServlet() {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");return servletRegistrationBean;}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), new TestServiceImpl());endpoint.publish("/TestService");return endpoint;}@Beanpublic Endpoint endpoint2() {EndpointImpl endpoint = new EndpointImpl(springBus(), new CatServiceImpl());endpoint.publish("/CatService");return endpoint;}}

启动项目。我的项目端口号是8080。浏览器访问地址:http://localhost:8082/webService
可见接口信息CatService和TestService,点进链接可以看每个接口的wsdl文档。

2、客户端开发

客户端是一个单独的项目。

(1)pom依赖

不同的SpringBoot版本对应的依赖版本也不一样,我也是试了好久终于成了。我的SpringBoot版本号是2.3.0.RELEASE。

  <!-- 进行jaxes 服务开发 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- 内置jetty web服务器 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency>

(2)封装客户端方法clientUtil

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class clientUtil {public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient(wsdUrl);//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));Object[] objects;// invoke("方法名",参数1,参数2,参数3....);objects = client.invoke(operationName, params);return objects[0].toString();}
}

(3)调用接口类

使用定时调用webservice接口。

import com.admin.webAppoint.webservice.client.clientUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;@RestController
@Component
public class TestController {//在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文。ClassLoader classLoader = Thread.currentThread().getContextClassLoader();@Scheduled(cron="*/15 * * * * ?")public String getMessage()  {Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文System.out.println("======开始调用webservice接口=====");String url = "http://localhost:8082/webService/CatService?wsdl";String methodName = "message";System.out.println("Calling" + url);String result="";try {result=clientUtil.callWebSV(url, methodName, "name");} catch (Exception e) {System.err.println("接口调用失败!!!!");return "失败";}System.out.println("===Finished!===恭喜你啊!!!CatService接口调用成功!!!===获得的数据是:"+result);return "Finished!";}@Scheduled(cron="*/5 * * * * ?")public String getMessage2()  {Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文System.out.println("======开始调用webservice接口=====");String url = "http://localhost:8082/webService/TestService?wsdl";String methodName = "sendMessage";System.out.println("Calling" + url);String result="";try {result=clientUtil.callWebSV(url, methodName, "username");} catch (Exception e) {System.err.println("接口调用失败!!!!");return  "失败";}System.out.println("===Finished!===恭喜你啊!!!TestService接口调用成功!!!===获得的数据是:"+result);return "Finished!";}
}

(4)运行结果

首先启动服务端。启动客户端。

遇到过报错:
报错——使用cxf时报错:org.apache.cxf.interceptor.Fault: Marshalling Error: XXX is not known to this context

最终成功调用服务端的webservice接口:

SpringBoot——实现WebService接口服务端以及客户端开发相关推荐

  1. SpringBoot整合WebService(服务端+客户端)

    SpringBoot整合WebService(服务端+客户端) 文章目录 SpringBoot整合WebService(服务端+客户端) 一.服务端 1.项目结构 2.创建好SpringBoot项目后 ...

  2. 《Netty权威指南 第2版》学习笔记(1)---服务端与客户端开发入门

    前言 Netty权威指南中以时间服务器为入门案例,演示了如何通过Netty完成了服务端与客户端之间的交互过程. 在开始使用Netty开发之前,先回顾一下使用NIO进行服务端开发的步骤. 创建Serve ...

  3. 简:webservice服务端及客户端开发

    一.服务端开发 1️⃣接口 public interface WsServer {String sayHello(String name); } 2️⃣实现类 import javax.jws.Web ...

  4. WebService服务端与客户端开发

    最近客户提出了将我们做的系统接入到他们系统之中,方便他们进行集中管理这个需求,其中主要就是运用了WebService技术来进行系统之间的接入.在此记录一下整个WebService是如何嵌入我们系统的以 ...

  5. Java服务端和客户端开发辅助工具Utils

    包括了各种工具类.辅助类.管理类等 Awesome_API: https://github.com/marktony/Awesome_API/blob/master/Chinese.md 收集中国国内 ...

  6. springboot+websocket实现服务端、客户端

    一.引言 小编最近一直在使用springboot框架开发项目,毕竟现在很多公司都在采用此框架,之后小编也会陆续写关于springboot开发常用功能的文章. 什么场景下会要使用到websocket的呢 ...

  7. XFire创建WebService服务端和客户端

    WebService 接口在java中的开发 接口化自己的程序方法可以将自己的实现类进行一定程度的封装,只提供接口方法给其它程序,如果其它程序需要调用这个接口,它不再需要关心具体的实现,数据的如何处理 ...

  8. Spring和CXF整合发布WebService(服务端、客户端)

    参考Spring和CXF整合发布WebService(服务端.客户端) 转载于:https://www.cnblogs.com/timspace/p/11113576.html

  9. SpringBoot(23) 集成socket.io服务端和客户端实现通信

    一.前言 websocket和socket.io区别? websocket 一种让客户端和服务器之间能进行双向实时通信的技术 使用时,虽然主流浏览器都已经支持,但仍然可能有不兼容的情况 适合用于cli ...

最新文章

  1. webview Java与JS互调
  2. Day13 目录结构自定义Yum仓库源码包编译安装(Service02)
  3. python调试网页_Python Django shell 调试
  4. C语言二进制转换为十六进制(附完整源码)
  5. va_start、va_end、va_list的使用
  6. PHP怎么检查登录和退出,如何检查用户是否以PHP登录?
  7. 139社区改版能给我们带来什么?
  8. CentOs 开启ssh服务
  9. 美国人因工程学的历史
  10. 十大经典排序算法--详解
  11. L1-005 考试座位号 (15 分)
  12. PLC通讯实现-C#实现AB5000 PLC串口通讯DTL32(七)
  13. 4月21日V反再次来临?
  14. 一个曾经纯数学专业毕业的未来算法工程师内心独白
  15. 解决更改mysql密码时报错Your password does not satisfy the current policy requirements问题
  16. 判断小米 魅族 华为 系统 MIUI EMUI FLYME
  17. 小程序基础开发(五):微信支付全套流程
  18. 一招解决苹果签名包掉包问题,一年只需签一次
  19. 最小二乘蒙特卡洛模拟方法-美式期权定价
  20. 【人工智能】传统机器学习算法(QDU)

热门文章

  1. [转载] Java static关键字与static{}语句块
  2. sdram trp_TRP的完整形式是什么?
  3. c语言存储类_C编程语言的存储类
  4. dcom配置_spring cloud 二代架构依赖组件 全配置放送
  5. access 导入txt 找不到可安装的isam_由浅入深:Python 中如何实现自动导入缺失的库?...
  6. Java类类getComponentType()方法与示例
  7. Zookeeper 的 5 大核心知识点!
  8. ubuntu搭建nodejs生产环境——快速部署手册
  9. Microsoft SQL Server 2005 提供了一些工具来监控数据库
  10. Gtk-WARNING : cannot open display----这个问题在NVIDIA TX2上碰到过就是DISPLAY=“:0“