zoukankan      html  css  js  c++  java
  • 使用 CXF、Spring、Maven 创建一个 SOAP 客户端

    上一篇文章我们介绍了使用 CXF 创建 SOAP Web服务,接下来我们来学习如何编写 SOAP 客户端。
    使用 CXF 有很多种不同的方法来创建 SOAP 客户端,本文介绍一种最快速、最简单的方法。
    首先从服务器上获取 wsdl 的 URL 地址。
    打开 pom.xml 文件:
    增加 wsdl-to-java 插件,该插件将根据 wsdl 生成对应调用服务的 Java 客户端代码。
    <plugin>
    <
    <groupId>org.apache.cxf</groupId>
    <
    <artifactId>cxf-codegen-plugin</artifactId>
    <
    <version>${cxf.version}</version>
    <
    <executions>
    <
    <execution>
    <
    <id>generate-sources</id>
    <
    <phase>generate-sources</phase>
    <
    <configuration>
    <
    <sourceRoot>generated/cxf</sourceRoot>
    <
    <wsdlOptions>
    <
    <wsdlOption>
    <
    <wsdl>http://localhost:7009/cxf-spring-test/calculatorService?wsdl</wsdl>
    <
    </wsdlOption>
    <
    </wsdlOptions>
    <
    </configuration>
    <
    <goals>
    <
    <goal>wsdl2java</goal>
    <
    </goals>
    <
    </execution>
    <
    </executions>
    <
    </plugin>
    然后打开命令行窗口,并进入项目所在目录,执行:

    mvn clean generate-sources

    这将生成 wsdl 对应的客户端代码,代码存放于 <Project Folder>/generated/cxf 目录.
    将该目录添加到项目的构建目录。
    接下来我们写一个简单的类。
    Application Context File:
    <?xml  version="1.0" encoding="UTF-8"?>
    <
    <beans xmlns="http://www.springframework.org/schema/beans"
    x
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    x
    xmlns:context="http://www.springframework.org/schema/context"
    x
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    x
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    x
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
    x
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    h
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    h
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    h
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    h
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    h
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    h
    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">
     
     
     
    <
    <!-- Start up bean which will invoke server webservice -->
    <
    <bean id="calculatorService" init-method="invoke">
    <
    <constructor-arg index="0" value="http://localhost:7009/cxf-spring-test/calculatorService?wsdl"/>
    <
    </bean>
     
     
     
    <
    </beans>
    Java 客户端类
    package com.test;
     
     
     
    i
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
     
     
     
    p
    public class CalculatorServiceClient
    {
    {
    p
    private String targetURL;
     
     
     
    p
    public CalculatorServiceClient(String targetURL)
    {
    {
    t
    this.targetURL = targetURL;
    }
    }
     
     
     
    p
    public void invoke()
    {
    {
    J
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    f
    factory.setServiceClass(CalculatorIntf.class);
     
     
     
    f
    factory.setAddress(this.targetURL);
    C
    CalculatorIntf calculatorIntf = (CalculatorIntf) factory.create();
     
     
     
    /
    //invoking add web servcie
    S
    System.out.println("10 + 15 = "+calculatorIntf.add(10, 15));
     
     
     
    /
    //invoking subtract web service
    S
    System.out.println("15 - 5 = "+calculatorIntf.subtract(15, 5));
     
     
     
    /
    //invoking multiply web service
    S
    System.out.println("10 * 5 = "+calculatorIntf.multiply(10,5));
    }
    }
    }
    }
    源码打包下载: cxf_spring_maven.zip
  • 相关阅读:
    [JLOI2010] 冠军调查
    [ZJOI2009] 狼和羊的故事
    [CF1451D] Circle Game
    [CF1451E1] Bitwise Queries (Easy Version)
    [CF343D] Water Tree
    [CF1344B] Monopole Magnets
    [CF191C] Fools and Roads
    [CF1370D] Odd-Even Subsequence
    [CF1366D] Two Divisors
    [CF1359D] Yet Another Yet Another Task
  • 原文地址:https://www.cnblogs.com/huapox/p/3251607.html
Copyright © 2011-2022 走看看