Search code examples
javaweb-servicesaxisjava-6

Dynamic Webservice Invocation with Url Arguments


I need to invoke existing web services without creating the clients. I used AXIS apache to generate a dynamic client like below.

try {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setEncodingStyle("targetNameSpace");

        call.setTargetEndpointAddress(new java.net.URL("WSDLURL"));
        call.setOperationName(new QName("targetNameSpace", "runJob"));

        call.addParameter("arg0", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);

        call.setReturnType(org.apache.axis.Constants.XSD_STRING);

        String result = (String) call.invoke(new Object[] {"--context=Tokens"});
        log.info("result: {}", result);
        if (!result.equals("0")){
            throw new MosApplicationError("Job Failed ");
        }

    } catch (Exception e){
        throw new MosApplicationError("Talend job failed", e);
    }

My wsdl does not use anything other than string for both request and response object. When running this code I get following error:

11:59:29,424 ERROR [Call] Exception:
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
    at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145)
    at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
    at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
    at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
    at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
    at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
    at org.apache.axis.client.Call.invoke(Call.java:2467)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.nex.mos.job.core.TalendJob.executeJob(TalendJob.java:56)
    at com.nex.mos.job.core.BaseJob.execute(BaseJob.java:69)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
11:59:29,496 ERROR [MosErrorHandler] Error processing job in group 'Temporary' implemented by class 'class org.quartz.JobDetail'.  Error message: Tal
end job failed
com.nex.mos.common.exception.MosApplicationError: Talend job failed
    at com.nex.mos.job.core.TalendJob.executeJob(TalendJob.java:63)
    at com.nex.mos.job.core.BaseJob.execute(BaseJob.java:69)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
    at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
    at org.apache.axis.client.Call.invoke(Call.java:2470)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.nex.mos.job.core.TalendJob.executeJob(TalendJob.java:56)

Caused by: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
    at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145)
    at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
    at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
    at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
    at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
    at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
    at org.apache.axis.client.Call.invoke(Call.java:2467)

Does anyone has any idea? Axis version is 1.4 and jdk 1.6

Thanks in advance.


Solution

  • I found the problem. Previously I was not able to read WSDL file properly. My wsdl was returning an array of string but in the code above I was defining the return type as string instead of array of string. So adding these two lines fixed my problem :)

            //Return object is array of string
            call.setReturnType(Constants.SOAP_ARRAY);
            //Added to marshal and de-marshal the array of string
            call.registerTypeMapping(String[].class, new QName(talendApplicationName,""), ArraySerializerFactory.class, ArrayDeserializerFactory.class);