Java调用soap协议的webservice

package test;

import org.apache.axis.utils.StringUtils;

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


public class CalculateClient {

    public static void main(String[] args) {
        // 指定调用WebService的URL(这里是我们发布后点击HelloWorld)
        String url = "http://localhost:8080/services/HelloWorld?wsdl";
        //调用的方法
        String method = "add";
        //调用方法的参数列表
        Object[] parms = new Object[]{2.0,3.0};
        CalculateClient calculateClient = new CalculateClient();
        //调用方法
        String svrAddResult = calculateClient.CallMethod(url, method, parms);

        System.out.println(svrAddResult);

        String svrMinusResult = calculateClient.CallMethod(url, "minus", new Object[]{5.0,2.0});
        String svrMultiplyResult = calculateClient.CallMethod(url, "multiply", new Object[]{2.0,3.0});
        String svrDivideResult = calculateClient.CallMethod(url, "divide", new Object[]{8.0,5.0});
        String svrPowerResult = calculateClient.CallMethod(url, "power", new Object[]{5.0});
        String svrSqrtResult = calculateClient.CallMethod(url, "sqrt", new Object[]{9.0});

        System.out.println("5.0 - 2.0 is " + svrMinusResult);
        System.out.println("2.0 * 3.0 is " + svrMultiplyResult);
        System.out.println("8.0 - 5.0 is " + svrDivideResult);
        System.out.println("5.0^2 is " + svrPowerResult);
        System.out.println("9.0^(1/2) is " + svrSqrtResult);
    }

    //实现WebService上发布的服务调用
    public String CallMethod(String url, String method, Object[] args) {
        String result = null;

        if(StringUtils.isEmpty(url)) {
            return "url地址为空";
        }
        if(StringUtils.isEmpty(method)) {
            return "method地址为空";
        }

        Call rpcCall = null;

        try {
            //实例websevice调用实例
            Service webService = new Service();
            rpcCall = (Call) webService.createCall();
            rpcCall.setTargetEndpointAddress(new java.net.URL(url));
            rpcCall.setOperationName(method);

            //执行webservice方法

            double rslt = (double) rpcCall.invoke(args);
            result = String.valueOf(rslt);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

}

https://amr.sz.gov.cn/szjxjgw/services/Spemainser?wsdl

package com.czh;

import org.apache.axis.utils.StringUtils;

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

/**
 * 调用服务的客户端
 */
public class CalculateClient {

    public static void main(String[] args) {
        // 指定调用WebService的URL(这里是我们发布后点击HelloWorld)
        String url = "https://amr.sz.gov.cn/szjxjgw/services/Spemainser?wsdl";
        //调用的方法
        String method = "changemainten";
        //调用方法的参数列表
        Object[] parms = new Object[]{"{'contenttype':'4','userinfo':{'userid':'AA12','timestamp':'2016-12-22 15:34:43','nonce':'abcdef','signature':'4330e2dcf44f131f258527ef002f5d0a85423f6c'},'content':{'maintnote':{'normalmainttime':'2016-06-10','mainttype':'1','mainttime':'2016-10-26 15:34:43','maintstaff':'1','maintstafftel':'1','notifytime':'2016-10-26 15:34:43','arrivetime':'2016-10-26 15:34:43','endtime':'2016-10-26 15:34:43','servicetype':'1','otherservice':'1','faultdesc':'1','resolveresult':'1','partscode':'1','deliverynum':'1','remark':'1','useorgopinion':'1','maintstaffid':'1','useorgstaff':'1','useorgname':'1','maintorgname':'1','status':'1','createdtime':'2016-06-10','ismakeup':'1','elevcode':'1','elevtype':'1','makeuptime':'2016-06-10','useorgcontime':'2016-06-10','useorgstaffid':'4545','maintorgcode':'444','useorgmanager':'444','useorgpone':'444','elevlongit':'444','elevlatit':'444'},'halfmonthmaintitem':{'hm1':'2','hm2':'2','hm3':'2','hm4':'2','hm5':'2','hm6':'2','hm7':'2','hm8':'2','hm9':'2','hm10':'2','hm11':'2','hm12':'2','hm13':'2','hm14':'2','hm15':'2','hm16':'2','hm17':'2'},'quartermaintitem':{'qtr1':'3','qtr2':'3','qtr3':'3','qtr4':'3','qtr5':'3','qtr6':'3','qtr7':'3','qtr8':'3','qtr9':'3','qtr10':'3'},'halfyearmaintitem':{'hy1':'4','hy2':'4','hy3':'4','hy4':'4','hy5':'4','hy6':'4','hy7':'4','hy8':'4','hy9':'4','hy10':'4'},'yearmaintitem':{'y1':'5','y2':'5','y3':'5','y4':'5','y5':'5','y6':'5','y7':'5','y8':'5','y9':'5','y10':'5','y11':'5','y12':'5','y13':'5','y14':'5'}}}"};
        CalculateClient calculateClient = new CalculateClient();
        //调用方法
        String svrAddResult = calculateClient.CallMethod(url, method, parms);

        System.out.println(svrAddResult);

    }

    //实现WebService上发布的服务调用
    public String CallMethod(String url, String method, Object[] args) {
        String result = null;

        if(StringUtils.isEmpty(url)) {
            return "url地址为空";
        }
        if(StringUtils.isEmpty(method)) {
            return "method地址为空";
        }

        Call rpcCall = null;

        try {
            //实例websevice调用实例
            Service webService = new Service();
            rpcCall = (Call) webService.createCall();
            rpcCall.setTargetEndpointAddress(new java.net.URL(url));
            rpcCall.setOperationName(method);

            //执行webservice方法

            String rslt =  (String)rpcCall.invoke(args);
            result = String.valueOf(rslt);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

}


<?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.czh</groupId>
  <artifactId>webserviceCall</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>webserviceCall</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.xfire</groupId>
      <artifactId>xfire-aegis</artifactId>
      <version>1.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.xfire</groupId>
      <artifactId>xfire-all</artifactId>
      <version>1.2.6</version>
      <exclusions>
        <exclusion>
          <groupId>org.codehaus.xfire</groupId>
          <artifactId>xfire-spring</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.xml.rpc/javax.xml.rpc-api -->
    <dependency>
      <groupId>javax.xml.rpc</groupId>
      <artifactId>javax.xml.rpc-api</artifactId>
      <version>1.1</version>
    </dependency>


    <dependency>
      <groupId>org.apache.axis</groupId>
      <artifactId>axis</artifactId>
      <version>1.4</version>
    </dependency>


  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

找不到引入的时候来这里查
https://mvnrepository.com/
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐