1

How do I make a java client to call a soap WebService method with parameters?

I've tried this class for a java client

import javax.xml.soap.*;

public class SOAPClientSAAJ {

public static void main(String args[]) throws Exception {
    // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    // Send SOAP Message to SOAP Server
    String url = "http:localhost:8080/myproject/mywebservice?wsdl";
    SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

    // print SOAP Response
    System.out.print("Response SOAP Message:");
    soapResponse.writeTo(System.out);

    soapConnection.close();
}

private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String namespace= "http://wsnamespace/";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("example", namespace);

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("Login", "example");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("username", "example");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("password", "example");
    soapBodyElem1.addTextNode("email@example.com");
    soapBodyElem2.addTextNode("1234");

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", namespace + "Login");

    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println();

    return soapMessage;
}

}

with this java webservice method

@WebMethod(operationName="Login")
public boolean Login(@WebParam(name = "username") String username,
@WebParam(name = "password") String password) {
  System.out.print(username + "-" + password);
}

but username and password are always null so the output when the method is called is "null-null", I would like to know how call this method sending the paramaters correctly.

thanks!

serch san
  • 11
  • 1
  • 4

1 Answers1

0

You can make a HTTPClient call and pass SOAP request as string parameter. There are better way to do this that internally does the above for example using Apache AXIS2, CXF, JAX-WS etc.

I prefer to use CXF by generating stubs of WSDL file and calling the service through JAVA. Refer examples:

Community
  • 1
  • 1
Amit Mahajan
  • 895
  • 6
  • 34