回忆调用WebService的过程,使用CXF调用

写这篇文章的初衷呢就是在工作中涉及和原始系统的数据同步交互,老的系统还是使用传统的WebService技术,正好把好多年前使用的技术在熟悉一遍,记得当初在WebService比较火的AIX2,JAX-WS、CXF,我选用的是CXF技术。

参考:http://cxf.apache.org/

调用天气例子:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

由于这个天气使用C#开发的WebService,java中解析不识别ref属性和HTML代码的标签,我采取的方法是将wsdl描述文件下载下来修改了一下,去掉相应的html代码,和一些ref属性使用cxf官方推荐的maven插件生成代码,当然也可以将cxf压缩包下载下来配置好环境变量,通过wsdl2java命令来生成相应的客户端代码,先把maven中的pom贴出来

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cloudyoung</groupId>
    <artifactId>simple-webservice</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>simple-webservice</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <cxf.version>2.2.3</cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>



    </dependencies>


    <build>
        <plugins>
            <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>${basedir}/src/main/java</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <!--QQ验证-->
                                    <wsdl>http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl</wsdl>
                                    <!--天气预报-->
                                    <!--<wsdl>http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl</wsdl>-->
                                    <!--<wsdl>${basedir}/src/main/resources/WeatherWebService.wsdl</wsdl>-->
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution><!---->
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

然后通过maven插件来生成相应的客户端代码

clean,package一下就可以生成。

//天气预报
        WeatherWebService weather = new WeatherWebService();
        ArrayOfString result = weather.getWeatherWebServiceSoap().getSupportProvince();
        System.out.println("支持的省份:");
        if(null != result && result.getString().size() > 0){
            result.getString().forEach(d -> System.out.println(d));
        }
        System.out.println("支持的城市:");
        ArrayOfString res = weather.getWeatherWebServiceSoap().getSupportCity("直辖市");
        if(null != res && res.getString().size() > 0){
            res.getString().forEach(d -> System.out.println(d));
        }
        System.out.println("北京的天气:");
        ArrayOfString weatherbyCityName = weather.getWeatherWebServiceSoap().getWeatherbyCityNamePro("北京", "00234");
        if(null != weatherbyCityName && weatherbyCityName.getString().size() > 0){
            weatherbyCityName.getString().forEach(d -> System.out.println(d));
        }

将方法写上,方便以后再遇到和老系统对接可以方便的查找。有什么不对的还希望大家能够多多的给指点!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值