axis2调用webservice 服务端报错First Element must contain the local name, Envelope , but found definitions

产生这个错误的原因是 因为服务端方法中 使用 @WebParam  声明了参数的名称,故客户端调用的需要加上参数名称 具体参考  使用  call.addParameter("queryZtMap", // 参数名
                   XMLType.XSD_STRING, // 参数类型:String
                   ParameterMode.IN); // 参数模式:'IN' or 'OU

package com.hyjx.business.clientdevice;


import java.rmi.RemoteException;


import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;


import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
/** 
* @author :hanzl 
* @version 创建时间:2018年1月26日 上午10:00:40 
*/
public class Testaxis2 {


public static void main(String[] args) throws RemoteException {
String url="http://ip:端口号/tzejclzx/ws/ztxx";
String namespace="http://service.framework.hyjx.com/";
String methodName="getZtMap";

Service service = new Service();
Call call = null;
String result2="";
try {
     call = (Call) service.createCall();
// 步骤3:设置目标地址,即需要访问的webservice地址
             call.setTargetEndpointAddress(url);
             // 步骤4:设置调用的方法名
             call.setOperationName(new QName(namespace, methodName));   
         // call.setOperationName("getZtMap");
         // 步骤5: 设置参数名
         call.addParameter("queryZtMap", // 参数名
                   XMLType.XSD_STRING, // 参数类型:String
                   ParameterMode.IN); // 参数模式:'IN' or 'OUT'
          // 步骤6:设置返回值类型
            call.setReturnType(XMLType.XSD_STRING); // 返回值类型:String
            String queryZtMap="00059433bf4243db85b3ce99ec9616ef";
            //步骤7 :调用call.invoke(Object[] obj)方法
          result2 = (String) call.invoke(new Object[] { queryZtMap });// 远程调用
          System.out.println(result2);
} catch (ServiceException e) {

}

}
 
}

另一种方式(不用@WebParam声明参数调用方式)一般我们调用的都是不用写服务端没有声明  参数的方式   即 不用声明参数的名称

 public String returSult(String url,Object[] obj,String namespace,String methodName) {
    String result="";
    RPCServiceClient client=null;
    try {
    client = new RPCServiceClient();
    Options options = client.getOptions();  
    //options.setTimeOutInMilliSeconds(1000*60*5);
    EndpointReference end = new EndpointReference(url);
    Class<?>[] classes = new Class[] { String.class};
    options.setTo(end);
    options.setManageSession(true);   
    options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true); 
        options.setProperty(org.apache.axis2.Constants.Configuration.DISABLE_SOAP_ACTION,true);
        QName qname = new QName(namespace, methodName);
        result = (String)client.invokeBlocking(qname, obj,classes)[0];
        client.cleanupTransport();
} catch (Exception e) {
result="连接服务器失败";
}finally{
if(client != null){
try {
//client.cleanup();
client.cleanupTransport();
} catch (AxisFault e) {
e.printStackTrace();
result="连接服务器失败";
}
}

    return result;  
    }

要依赖的jar包Maven如下

<!-- hanzl axis2 jar -->
<dependency>
    <groupId>commons-discovery</groupId>
    <artifactId>commons-discovery</artifactId>
    <version>0.2</version>
</dependency>
<dependency>
    <groupId>javax.xml</groupId>
    <artifactId>jaxrpc</artifactId>
    <version>1.1</version>
   </dependency>
<dependency>
      <groupId>org.apache.axis</groupId>
       <artifactId>axis</artifactId>
       <version>1.4</version>
        </dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.ksoap2</groupId>
<artifactId>ksoap2-android-assembly</artifactId>
<version>3.2.0</version>
<classifier>dependencies</classifier>
</dependency>


<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>


<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</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>3.1.11</version>
</dependency>
<!-- cxf结束 -->
<!-- hanzl结束 -->


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值