上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService。另外本来想写一篇 xfire 作为 client 端来调用 webservice 的代码。后来发现 xfire 在2007 你那巅峰时刻,已经不再更新,而后来的 Spring 已经抛弃了部分 Api。导致现在已经不兼容了。

本项目源码 github 下载

1 新建 Spring Boot Maven 示例工程项目

注意:是用来 IDEA 开发工具

  1. File > New > Project,如下图选择 Spring Initializr 然后点击 【Next】下一步
  2. 填写 GroupId(包名)、Artifact(项目名) 即可。点击 下一步

    groupId=com.fishpro

    artifactId=webserviceclient
  3. 选择依赖 Spring Web Starter 前面打钩。
  4. 项目名设置为 spring-boot-study-webserviceclient.

2 引入依赖 Pom

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

3 编写调用代码

  1. 实例化 JaxWsDynamicClientFactory
  2. 建立Client请求
  3. invoke方法入口,并传递参数

http://localhost:8080/ws/user?wsdl 为上一章我们编写的示例。


@SpringBootApplication
public class WebserviceclientApplication { public static void main(String[] args) {
SpringApplication.run(WebserviceclientApplication.class, args); JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance(); Client client=dcflient.createClient("http://localhost:8080/ws/user?wsdl");
try{
Object[] objects=client.invoke("getUserById","1");
System.out.println("getUserById 调用结果:"+objects[0].toString()); Object[] objectall=client.invoke("getUsers");
System.out.println("getUsers调用部分结果:"+objectall[0].toString()); }catch (Exception e){
e.printStackTrace();
}
} }

右键 WebserviceclientApplication 点击 Run WebserviceclientApplication

2019-08-12 18:42:42.341  INFO 63593 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8087 (http) with context path ''
2019-08-12 18:42:42.344 INFO 63593 --- [ main] c.f.w.WebserviceclientApplication : Started WebserviceclientApplication in 2.527 seconds (JVM running for 3.132)
getUserById 调用结果:com.youdomain.webservice.UserDto@3c7d8a4
getUsers调用部分结果:[com.youdomain.webservice.UserDto@4e2824b1, com.youdomain.webservice.UserDto@534d0e20, com.youdomain.webservice.UserDto@7d18338b, com.youdomain.webservice.UserDto@3f4a605f]

本项目源码 github 下载

Spring Boot 使用 CXF 调用 WebService 服务的更多相关文章

  1. Spring Boot 使用 JAX-WS 调用 WebService 服务

    除了 CXF 我们还可以使用 Spring Boot 自身默认的组件 JAX-WS 来实现 WebService 的调用. 本项目源码 github 下载 1 新建 Spring Boot Maven ...

  2. cxf 调用 webservice服务时传递 服务器验证需要的用户名密码

    cxf通过wsdl2java生成客户端调用webservice时,如果服务器端需要通过用户名和密码验证,则客户端必须传递验证所必须的用户名和密码,刚开始想通过url传递用户名和密码,于是在wsdl文件 ...

  3. apache cxf笔记之Spring客户端访问和调用webservice服务

    继续上次的spring服务的问题,这边介绍一种spring配置客户端的方法. 1.src目录下新建client-beans.xml文件 <?xml version="1.0" ...

  4. Spring boot 整合CXF webservice 遇到的问题及解决

    将WebService的WSDL生成的代码的命令: wsimport -p com -s . com http://localhost:8080/service/user?wsdl Spring bo ...

  5. CXF发布webService服务以及客户端调用

    这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...

  6. spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法

    spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...

  7. SpringBoot | 第三十四章:CXF构建WebService服务

    前言 上一章节,讲解了如何使用Spring-WS构建WebService服务.其实,创建WebService的方式有很多的,今天来看看如何使用apache cxf来构建及调用WebService服务. ...

  8. 使用apache cxf实现webservice服务

    1.在idea中使用maven新建web工程并引入spring mvc,具体可以参考https://www.cnblogs.com/laoxia/p/9311442.html; 2.在工程POM文件中 ...

  9. 【转】构建基于CXF的WebService服务

    构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...

随机推荐

  1. 计算系数(NOIP2011提高LuoguP1313)

    一道数论好题,知识点涉及扩展欧几里得,快速幂,逆元,二项式定理,模运算,组合数等. (别问为啥打了快速幂不用费马小求逆元...我就练习下扩欧) (数据就应该再加大些卡掉n^2递推求组合数的) #inc ...

  2. 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传

    一.Flutter image_picker 实现相机拍照和相册选择   https://pub.dev/packages/image_picker   二.Flutter 上传图片到服务器   ht ...

  3. java程序启动参数

    例如 启动进程如下 /home/work/noah/ccs/jc-controller/jdk1.7.0_55/bin/java -Xmx4096m -Xms4096m -Xmn1024m -XX:+ ...

  4. Iris项目结构

  5. LED Decorative Light Manufacturer - Decorative Lighting: Functionality And Aesthetics

    Whether it is for general ambient lighting, task lighting or accent lighting, the decorative lightin ...

  6. [HDU5382]GCD?LCM!

    Description HDU5382 会吗?不会! 设\(F(n)=\sum\limits_{i = 1}^{n}\sum\limits_{j=1}^{n}[lcm(i,j)+gcd(i,j)\ge ...

  7. [Note]后缀自动机

    后缀自动机 代码 #include <cstdio> #include <algorithm> #include <cstring> const int M = 1 ...

  8. Oracle使用记录

    1.连接数据库的方式 sqlplus system/system@127.0.0.1:1521/orcl #远程用户名密码连接 sqlplus dbsnmp/oracle; #本地用户名密码连接 sq ...

  9. MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.

    MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option s ...

  10. EntityFramework使用SqlCe数据库

    使用NuGet添加下面3个引用 App.Config的<entityFramework>节点这样配置: <entityFramework> <defaultConnect ...