上一张我们讲到 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. rpm命令怎么指定安装位置

    rpm   -ivh    --prefix=路径      FILE.rpm

  2. object转为string

    object为null时 错误:object.tostring() 空指针 正确:(string)object

  3. codeforces Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

    #include<bits/stdc++.h> using namespace std ; int n; struct City { int id; long long x,y; //坐标 ...

  4. testng的prioriy

    todo: 同一个class中的priority: 1.不标priority的case和标注priority的case,谁先谁后? 2.标注相同priority的case,谁先谁后?是不是并发? 3. ...

  5. 使用Idea构建springmvc框架,出现no bean named 'cacheManager' is defined 错误

    由于IDEA的自动补全功能非常强大,当你配置 <mvc:annotation-driven/> 后编译器会帮你自动补全上面两个配置文件约束.这个时候如果你没注意的就会爆出一个很莫名奇妙的错 ...

  6. php提供下载服务实例

    两个步骤:1,通过header头信息告诉浏览器,我给你回应的是一个附件请接收 2,通过php读取下载的文件的内容并返回 前端 <!DOCTYPE html> <html lang=& ...

  7. Redis07——Redis五大数据类型 set

    set Redis中set可以自动排重(不会出现重复数据),提供了判断某个成员是否在一个set集合内的重要接口(list没有此功能) 无序集合,底层是一个value为null的hash表,添加.删除. ...

  8. Java - 集合 - List

    1.List实现类:ArrayList.LinkedList.Vector ArrayList使用: void test() { //声明 List<String> testlist = ...

  9. kill pkill

    首先说一下kill命令,它是通过pid(进程ID)来杀死进程,要得到某个进程的pid,我们可以使用ps(process status)命令,默认情况下,kill命令发送给进程的终止信号是15,但是有些 ...

  10. Reinforcement Learning Algorithm 资源

    算法源码: https://github.com/ljpzzz/machinelearning https://github.com/imraviagrawal/Reinforcement-Learn ...