使用 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>

然后打开命令行窗口,并进入项目所在目录,执行:

1mvn 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"
03xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
04xmlns:context="http://www.springframework.org/schema/context"
05xmlns:jee="http://www.springframework.org/schema/jee"xmlns:lang="http://www.springframework.org/schema/lang"
06xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
07xmlns:util="http://www.springframework.org/schema/util"xmlns:jaxws="http://cxf.apache.org/jaxws"
08xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
09http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
10http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
11http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
12http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
13http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
14http://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 客户端类

view source
print?
01package com.test;
02 
03import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
04 
05public class CalculatorServiceClient
06{
07private String targetURL;
08 
09public CalculatorServiceClient(String targetURL)
10{
11this.targetURL = targetURL;
12}
13 
14public void invoke()
15{
16JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
17factory.setServiceClass(CalculatorIntf.class);
18 
19factory.setAddress(this.targetURL);
20CalculatorIntf calculatorIntf = (CalculatorIntf) factory.create();
21 
22//invoking add web servcie
23System.out.println("10 + 15 = "+calculatorIntf.add(1015));
24 
25//invoking subtract web service
26System.out.println("15 - 5 = "+calculatorIntf.subtract(155));
27 
28//invoking multiply web service
29System.out.println("10 * 5 = "+calculatorIntf.multiply(10,5));
30}
31}

源码打包下载: cxf_spring_maven.zip

原创粉丝点击