使用 CXF、Spring、Maven 创建一个 SOAP 客户端
来源:互联网 发布:多梦疲劳知乎 编辑:程序博客网 时间:2024/04/30 20:42
上一篇文章我们介绍了使用 CXF 创建 SOAP Web服务,接下来我们来学习如何编写 SOAP 客户端。
使用 CXF 有很多种不同的方法来创建 SOAP 客户端,本文介绍一种最快速、最简单的方法。
首先从服务器上获取 wsdl 的 URL 地址。
打开 pom.xml 文件:
增加 wsdl-to-java 插件,该插件将根据 wsdl 生成对应调用服务的 Java 客户端代码。
01
<
plugin
>
02
<
groupId
>org.apache.cxf</
groupId
>
03
<
artifactId
>cxf-codegen-plugin</
artifactId
>
04
<
version
>${cxf.version}</
version
>
05
<
executions
>
06
<
execution
>
07
<
id
>generate-sources</
id
>
08
<
phase
>generate-sources</
phase
>
09
<
configuration
>
10
<
sourceRoot
>generated/cxf</
sourceRoot
>
11
<
wsdlOptions
>
12
<
wsdlOption
>
13
<
wsdl
>http://localhost:7009/cxf-spring-test/calculatorService?wsdl</
wsdl
>
14
</
wsdlOption
>
15
</
wsdlOptions
>
16
</
configuration
>
17
<
goals
>
18
<
goal
>wsdl2java</
goal
>
19
</
goals
>
20
</
execution
>
21
</
executions
>
22
</
plugin
>
然后打开命令行窗口,并进入项目所在目录,执行:
1
mvn clean generate-sources
这将生成 wsdl 对应的客户端代码,代码存放于 <Project Folder>/generated/cxf 目录.
将该目录添加到项目的构建目录。
接下来我们写一个简单的类。
Application Context File:
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
04
xmlns:context
=
"http://www.springframework.org/schema/context"
05
xmlns:jee
=
"http://www.springframework.org/schema/jee"
xmlns:lang
=
"http://www.springframework.org/schema/lang"
06
xmlns:p
=
"http://www.springframework.org/schema/p"
xmlns:tx
=
"http://www.springframework.org/schema/tx"
07
xmlns:util
=
"http://www.springframework.org/schema/util"
xmlns:jaxws
=
"http://cxf.apache.org/jaxws"
08
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
09
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
10
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
11
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
12
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
13
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
14
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
15
16
<!-- Start up bean which will invoke server webservice -->
17
<
bean
id
=
"calculatorService"
init-method
=
"invoke"
>
18
<
constructor-arg
index
=
"0"
value
=
"http://localhost:7009/cxf-spring-test/calculatorService?wsdl"
/>
19
</
bean
>
20
21
</
beans
>
Java 客户端类
01
package
com.test;
02
03
import
org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
04
05
public
class
CalculatorServiceClient
06
{
07
private
String targetURL;
08
09
public
CalculatorServiceClient(String targetURL)
10
{
11
this
.targetURL = targetURL;
12
}
13
14
public
void
invoke()
15
{
16
JaxWsProxyFactoryBean factory =
new
JaxWsProxyFactoryBean();
17
factory.setServiceClass(CalculatorIntf.
class
);
18
19
factory.setAddress(
this
.targetURL);
20
CalculatorIntf calculatorIntf = (CalculatorIntf) factory.create();
21
22
//invoking add web servcie
23
System.out.println(
"10 + 15 = "
+calculatorIntf.add(
10
,
15
));
24
25
//invoking subtract web service
26
System.out.println(
"15 - 5 = "
+calculatorIntf.subtract(
15
,
5
));
27
28
//invoking multiply web service
29
System.out.println(
"10 * 5 = "
+calculatorIntf.multiply(
10
,
5
));
30
}
31
}
源码打包下载: cxf_spring_maven.zip
- 使用 CXF、Spring、Maven 创建一个 SOAP 客户端
- cxf整合spring、maven项目创建(客户端)
- 采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用
- 使用CXF和Spring发布Soap服务
- 使用CXF+spring创建一个web的接口项目
- 使用CXF+spring+restful创建一个web的接口项目
- 使用CXF+spring+restful创建一个web的接口项目
- 使用CXF+spring+restful创建一个web的接口项目
- 使用CXF+spring+restful创建一个web的接口项目
- 使用CXF 开发SOAP 的webService接口客户端
- 使用 Eclipse 创建 Maven SOAP Web 服务
- 使用maven搭建Spring整合cxf
- Apache CXF Spring SOAP example
- CXF+Spring 发布SOAP服务
- WEB项目 CXF与Spring整合服务端和客户端创建
- CXF创建webservice服务端、客户端,与spring集成
- CXF + spring 创建websevice 服务端客户端完整示例
- 使用Swift创建一个SOAP的请求
- 11714 - Blind Sorting
- vector,deque,list,map,数组比较与分析
- 黑马程序员----JAVA----枚举和反射
- 使用 Eclipse 创建 Maven SOAP Web 服务
- 20130226日报(网易笔试有感)
- 使用 CXF、Spring、Maven 创建一个 SOAP 客户端
- 黑马程序员----JAVA----注解
- 深入理解JavaScript闭包(closure)
- Hadoop编程之MapReduce操作Mysql数据库
- 黑马程序员----JAVA----交通灯
- NYOJ - A problem is easy
- 深入浅出json
- 安全设备能否实现弹性计算
- 作为程序员最重要的是什么