最近一个项目中需要用dotnet来调用java webservice,就是要将hp 的service desk里的wap_api.jar文件中的API封装成java webservice(这里的API都是.class文件) ,然后让dotnet来调用。其中有个opensession的静态方法是通过ip地址和帐号来连接服务器并返回一个自定义对象,由于java webservice中不能传递静态方法,于是在java中新建立了一个类,继承于包含opensession静态方法的类ApiSDSession(该类中包含大量的接口类的引用和属性),将这个静态方法重新封装成了实例方法OpenNewSession 供dotnet客户端调用,由于原来静态方法返回的是个自定义类型ApiSDSession,因此在定义这个实例方法的时候就这样写 :
....
public ApiSDSession OpenNewSession(String host,String account,String password)
{
    return super.opensession(host,account,password);
}在myeclipse6.6中建立web service project,建立的时候选择的是XFire框架,然后建立类NewSession,该类继承自wap_api.jar文件中的一个类ApiSDSession ,在这个新建类NewSession中包含上述封装静态方法的方法。打算将这个类及其父类中的方法封装成java webservice。于是接下来建立webservice,依然是选择xfire,选择通过类来创建webservice,然后输入webservice的名字,不使用接口,直接选择这个新建的类,完成。然后run as tomcat,在tomcat服务器上发布这个webservice,输入地址:http://localhost:8080/MyTest/services/NewSessionWS?wsdl,查看wsdl文件可以显示。于是在dotnet里添加web引用,最后在dotnet里调用这个OpenNewSession方法来连接一个服务器。
dotnet中的代码:Service service=new Service();
service.OpenNewSession(服务器ip地址,用户帐号,用户密码);在执行这个连接方法的时候,dotnet 抛出一个soapheaderexception 的异常,提示:java.lang.String cannot be cast to java.util.map 的提示,tomcat也出现这个异常信息,这个异常让我很疑惑了好几天,也不知道如何来处理这个错误问题?我上述的操作过程是否有误?请大家帮忙!!我用的是jdk5.0,jdk6.0,换着用,都不解决问题。另外说明一点,如果不使用webservice ,直接在java中使用上述的OpenNewSession方法去连接服务器,运行正常。

解决方案 »

  1.   

    你在JAVA中调用过这个webservice吗,能成功吗?
      

  2.   

    java中调用我倒没有测试过。刚接触用java建立webservice,以前都用dotnet来建立webservice的。有啥好方法解决这个问题啊????
      

  3.   

    我用XFIRE写webservice一般都是先在JAVA上调用通过,再发布的。你有没有在webservice的参数中使用java的map类型呀,如果使用了,.net不一定能用哟。java的调用方法:    Service srvcModel = new ObjectServiceFactory()
                    .create(Iquery.class);
            XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
                    .newInstance().getXFire());
                    
                      
             
     
          
            String helloWorldURL = "http://172.17.99.20/yygh/services/Query";
          
            try {
             Iquery srvc = (Iquery) factory.create(
                        srvcModel, helloWorldURL);
              
     
             Client client = ((XFireProxy)Proxy.getInvocationHandler(srvc)).getClient();
             
             out.println("yygh:"+srvc.GetMzYygh("2008-05-01 00:00:00","2008-05-31 00:00:00").size());   } catch (MalformedURLException e) {
                e.printStackTrace();
            }
      

  4.   

    没有用过XFire  只用过axis
    好像myeclipse集成了XFire
      

  5.   

    我所要封装的成webservice的API是hp公司提供的,都是class文件,我只是在外部使用这个连接的方法,传递了3个string类型的参数,没有传别的啊.
      

  6.   

    我将相关的一些class文件反编译了一下,其中涉及到了一个HashTable的变量,不知道是否跟他有关系,现将相关代码贴出来:
    .........public class ApiSDSession extends ApiSession
        implements IApiSession
    {    protected ApiSDSession()
        {
        }    protected ApiSDSession(ApiLoginResult apiloginresult)
        {
            super(apiloginresult);
        }    public IAccount getCurrentAccount()
        {
            return (IAccount)((ApiHome)getAccountHome()).entityOpen(new Long(account));
        }    public static ApiSDSession openSession(String s, String s1, String s2)
        {
            return new ApiSDSession(ApiSession.connect(s, s1, s2));
        }
    }.........
    public class ApiSession implements IApiSession
    {    protected ApiSession()
        {
            homes = new Hashtable();
        }    protected int workflowSession;
        protected long account;
        private Hashtable homes;
        protected String className;
        protected ApiRoleInfo roleInfo;
        protected int locale;
        protected int daysUntilPasswordExpires;
        public static final String REVISION = "$Revision: 17 $";
        static Class class$com$hp$ifc$apiserver$ps$IApiServerSession; /* synthetic field */
        static Class class$com$hp$ifc$bus$ps$IAppSrvDispatch; /* synthetic field */
        static Class class$com$hp$ifc$api$IApiSession; /* synthetic field */    protected ApiSession(ApiLoginResult apiloginresult)
        {
            homes = new Hashtable();
            workflowSession = apiloginresult.getSessionId();
            account = apiloginresult.getAccountId();
            locale = apiloginresult.getLocale();
            daysUntilPasswordExpires = apiloginresult.getDaysUntilPasswordExpiration();
            roleInfo = new ApiRoleInfo(this);
            ApiORB.addSession(apiloginresult.getSessionId(), apiloginresult.getServer());
        }protected static ApiLoginResult connect(String s, String s1, String s2)
        {
            try
            {
                AppORB.setDefaultServerAddress(s);
            }
            catch(UnknownHostException unknownhostexception)
            {
                throw new RuntimeException(unknownhostexception.toString());
            }
            IApiServerSession iapiserversession = (IApiServerSession)AppORB.proxyOf(class$com$hp$ifc$apiserver$ps$IApiServerSession != null ? class$com$hp$ifc$apiserver$ps$IApiServerSession : (class$com$hp$ifc$apiserver$ps$IApiServerSession = class$("com.hp.ifc.apiserver.ps.IApiServerSession")));
            ApiLoginResult apiloginresult = iapiserversession.login(s1, s2);
            return apiloginresult;
        }}
      

  7.   

    在java中调用这个java webservice 返回一个异常
    使用的过程是:在myeclipse中建立一个web service project 然后建立一个web service client,在配置中选择通过wsdl文件来生成,输入地址:http://localhost:8080/ApiSessionTest/services/sessionWS?wsdl
    下一步对wsdl的验证出现了一个异常信息的列表(列表里有5,6条信息),忽略该异常,完成创建.编写测试代码:sessionWSPortType service = client.getsessionWSHttpPort();
            
    //TODO: Add custom client code here
             //
             //service.yourServiceOperationHere();
            System.out.println(service.sayHello("world"));        //此句可以正常显示出来.         service.newConnection(服务器地址, 用户名,用户密码);      //此句就出现下面的异常信息在java中调用这个java webservice 返回一个异常,如下:Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Error invoking 'apisessiontest.NewConnection(java.lang.String, java.lang.String, java.lang.String)'. Nested exception is java.lang.reflect.InvocationTargetException: null
    org.codehaus.xfire.fault.XFireFault: Error invoking 'apisessiontest.NewConnection(java.lang.String, java.lang.String, java.lang.String)'. Nested exception is java.lang.reflect.InvocationTargetException: null
    at org.codehaus.xfire.fault.Soap11FaultSerializer.readMessage(Soap11FaultSerializer.java:31)
    at org.codehaus.xfire.fault.SoapFaultSerializer.readMessage(SoapFaultSerializer.java:28)
    at org.codehaus.xfire.soap.handler.ReadHeadersHandler.checkForFault(ReadHeadersHandler.java:111)
    at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:67)
    at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
    at org.codehaus.xfire.client.Client.onReceive(Client.java:406)
    at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
    at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
    at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
    at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
    at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
    at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
    at org.codehaus.xfire.client.Client.invoke(Client.java:336)
    at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
    at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
    at $Proxy12.newConnection(Unknown Source)
    at my.client.sessionWSClient.main(sessionWSClient.java:100)