Aqif Hamid
Aqif Hamid

Reputation: 3521

How to pass parameters through a SOAP Message to consume a parameterized method of a webservice

I have written a java SOAP WebService. and a consumer as well. if I send a SOAP message to a method with no parameters. it works all fine, a proper response is recieved.

But, I am unable to consume a method that have parameters. My SOAP message is stored in following string in following pattern.

 String xml =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
                            "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
                                "<S:Header/>"+
                                "<S:Body>"+
                                    "<ns2:addPerson xmlns:ns2=\"http://service.cass.com/\">"+
                                        "<fName xsi:type=\"xsd:string\">vbn</fName>"+
                                        "<lName xsi:type=\"xsd:string\">yyyy</lName>"+
                                        "<gender xsi:type=\"xsd:string\">879</gender>"+
                                        "<age xsi:type=\"xsd:int\">90</age>"+
                                    "</ns2:addPerson>"+
                                "</S:Body>"+
                            "</S:Envelope>";

method prototype is: public boolean addPerson(String fName, String lName, String gender, int age);

and I am getting following exception.

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/ServerSide/ws/personService
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
    at com.cass.testRequest.makeSOAPRequest(testRequest.java:71)
    at com.cass.testRequest.main(testRequest.java:37)

Please notice that, if I send a SOAPMessage with no parameters, for a method with 0 parameters. It all works fine and I get a proper response. In my opinion, Something is wrong with the way I am passing parameters in SOAPMessage. Please suggest how to do that.

regards, Aqif

Upvotes: 0

Views: 10153

Answers (1)

DNA
DNA

Reputation: 42607

You haven't defined the xsi or xsd namespaces. Try something like the following (but see comments below about correct veriosn of the namespaces):

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"  
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/1999/XMLSchema">

(These wouldn't be needed for your method with no parameters, which is why it worked in that case).

Edited: The following validates as correct XML at http://validator.w3.org/check

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <S:Header/>
    <S:Body>
        <ns2:addPerson xmlns:ns2="http://service.cass.com/">
        <fName xsi:type="xsd:string">vbn</fName>
        <lName xsi:type="xsd:string">yyyy</lName>
        <gender xsi:type="xsd:string">879</gender>
        <age xsi:type="xsd:int">90</age>
        </ns2:addPerson>
    </S:Body>
</S:Envelope>

though that doesn't mean it conforms to the SOAP schema...that would be the next thing to check...

You may get problems if the client is using SOAP 1.1 and server is using SOAP 1.2 for example as I think the namespaces are different. Similarly, do not mix namespaces from the two versions - it must all be consistent.

And I think the up-to-date namespaces for xsd and xsi are now 2001 not 1999, (my mistake, I was using an old example).

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

But refer to the specs for SOAP 1.1 or 1.2 (whichever you are using) for the definitive namespaces!

Upvotes: 1

Related Questions