欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axis1.x调webservice程序

程序员文章站 2022-03-04 15:20:33
...

 

   最近在项目中遇到需要去调webservice的接口,我以前很少写,这里写一个例子,分享出来!没太大技术含量,不喜勿喷!!!

 1.pom.xml添加依赖包

   

<?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.xiaoyetanmodules</groupId>
    <artifactId>webserviceClient</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>webserviceClient</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
    </properties>

    <dependencies>
        <!--最近迷上了jfinal-->
        <dependency>
            <groupId>jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>2.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/jfinal-2.0-bin-with-src.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>cos</artifactId>
            <version>26Dec2008</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>

        <!--调webservice用的-->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-wsdl4j</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

    <build>
        <outputDirectory>${project.basedir}/src/main/webapp/WEB-INF/classes/</outputDirectory>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!-- Compiler 插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

            <!-- war 打包插件, 设定war包名称不带版本号 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                </configuration>
            </plugin>

            <!-- resource插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <!-- install插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
            </plugin>

            <!-- clean插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
            </plugin>

            <!-- ant插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>

            
    </build>
    <repositories>
        <repository>
            <id>nexus-aliyun</id>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
    </repositories>


</project>

 

2.core code

  

package com.xiaoyetan;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import java.net.URL;

/**
 * @Author xiaoyetan
 * @Date :created on 13:18 2017/8/24
 */
public class AxisClient {
    public static void main(String[] args) {
        String url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl" ;
        //创建客户端调用webservice的代理对象
        Service service = new Service();
        try {
            //创建一个调用对象,代表对web service 的一次调用
            Call call = (Call) service.createCall();
            //设置web service的url 地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //设置操作名称,QName 对象的两个参数分别为命名空间和方法名称
            call.setOperationName(new QName("http://WebXml.com.cn/","qqCheckOnline"));
            //不加这行会抛异常System.Web.Services.Protocols.SoapException: 服务器未能识别 HTTP 头 SOAPAction 的值:
            call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline");
            //传参
            call.addParameter(new QName("http://WebXml.com.cn/", "qqCode"), XMLType.XSD_STRING, ParameterMode.IN);
            //设置返回值类型                                                         
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            // 执行调用操作,result 保存返回的结果,invoke 的参数为实参
            String result = (String) call.invoke(new Object[]{"1160500991"});
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

 

就这么愉快的结束了!!!很简单