axis1.x adjusts the webservice program

 

   Recently, I encountered the interface that needs to adjust the webservice in the project. I rarely wrote it before. Here is an example and share it! Not too technical content, don't spray if you don't like it!!!

 1.pom.xml add dependency package

   

<?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>
        <!--Recently obsessed with 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>

        <!--Used to call 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 plugin, set JDK version -->
            <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 packaging plugin, set the war package name without version number-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                </configuration>
            </plugin>

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

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

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

            <!-- ant plugin-->
            <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" ;
        //Create a proxy object for the client to call the webservice
        Service service = new Service();
        try {
            //Create a call object representing a call to the web service
            Call call = (Call) service.createCall();
            //Set the url address of the web service
            call.setTargetEndpointAddress(new java.net.URL(url));
            //Set the operation name, the two parameters of the QName object are namespace and method name respectively
            call.setOperationName(new QName("http://WebXml.com.cn/","qqCheckOnline"));
            //If this line is not added, an exception will be thrown System.Web.Services.Protocols.SoapException: The server does not recognize the value of the HTTP header SOAPAction:
            call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline");
            // pass parameters
            call.addParameter(new QName("http://WebXml.com.cn/", "qqCode"), XMLType.XSD_STRING, ParameterMode.IN);
            //Set the return value type                                                         
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            // Execute the call operation, result saves the returned result, and the parameter of invoke is the actual parameter
            String result = (String) call.invoke(new Object[]{"1160500991"});
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace ();
        }
    }
}

 

 

It's such a happy ending! ! ! very simple 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326978722&siteId=291194637