关于java调用webservice参数传递为空问题

本文介绍了在Java中使用Myeclipse和JDK1.7调用VS2012创建的WebService时遇到的参数传递为空问题。通过使用Apache Axis库的Call类成功解决了问题。关键步骤包括设置命名空间匹配、指定SoapBindingUse.Literal以及使用SoapDocumentService注解。对于其他方法如RPCServiceClient和axis2生成的stub未能成功,可能是由于IDE或命名空间匹配问题导致。

    通过Myeclipse10.0 jdk1.7调用VS2012 webService遇到java客户端参数传递不过去的问题,搞了一下午终于出结果了,其实网上好多方法都只是一部分,需要综合一下。


客户端我是用import org.apache.axis.client.Call;方法调用服务成功解决该问题的的,通过RPCServiceClient以及axis2自动生成的stub调用均未找到合适的传递参数的方法


client:

import java.net.MalformedURLException;
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;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class test4 {
 public static void main(String[] args) throws RemoteException, MalformedURLException, ServiceException {
  String service_url = "http://localhost:30332/WebService1.asmx";
  Service ser = new Service();
  Call call = (Call) ser.createCall();
  call.setTargetEndpointAddress(new java.net.URL(service_url));
  QName opAddEntry = new QName("http://tempuri.org/", "HelloWorld");
  call.setOperationName(opAddEntry);
  call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
  call.setReturnType(XMLType.XSD_STRING);

  Object[] opAddEntryArgs = new Object[] { "jone" };
  String result = (String) call.invoke(opAddEntryArgs);
  call.invoke("http://tempuri.org/", "HelloWorld", opAddEntryArgs);
  System.out.println("result:" + result);
}
}
服务端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Diagnostics;
namespace WebApplication1
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
    // [System.Web.Script.Services.ScriptService]
    [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
    public class WebService1 : System.Web.Services.WebService
    {
      [SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/HelloWord", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
      [WebMethod]
        public string HelloWorld(string name)
        {
          //Console.WriteLine("name:"+name);
          //Console.ReadKey();
            Debug.WriteLine("NAME:"+name);
          return "hello"+name;
        }
       [SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/add", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
        [WebMethod]
        public int add(int x, int y)
        {
            return x + y;
        }
      
    }
}

调用结果:

result:hellojone


1 call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
  call.setReturnType(XMLType.XSD_STRING);

2[SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/HelloWord", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]        (c#中每个方法前都要添加这样的声明,否则会报错)

这两步是必要条件缺一不可,貌似是用以匹配二者的命名空间

 

3[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]

本方法加在类之前,旨在防止java调用时出现无法识别 soapAction的问题

PS:1 同学用eclipse做客户端直接用exis2自动生成的stub操作即可调用成功

HelloWorld helloWorld = new HelloWorld();
  helloWorld.setName("jone");
  HelloWorldResponse re=stub.HelloWorld(helloWorld);

System.out.println( re.getHelloWorldResult());

但是我跟他C/S代码完全相同却出现这个问题,说明,跟客户端IDE有关。

        2通过RPCServiceClient以及axis2自动生成的stub调用如果可以通过某种API实现两边的命名空间一致,或许也能传递成功,这里我并未深究

        3 按原理通过axis2生成stub调用时正确利用了服务器端的wsdl实现的调用功能,但这里苦于调用java编辑的服务器webservice抛出wrong number of arguments,调用C#webservice出现参数传递失败问题,目前仅能用本帖方法实现,这也可可能跟IDE有关,同学用eclipse就为出现此类情况。。。请大家引以为戒

 

 

 


 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值