Java and Soap

Since JDK 6 you have built in support for SOAP (SAAJ ).

There are many services that still use it, therefore consuming soap service is always usefull.

By creating a SOAP message you can add some attributes to the evlelop.

    try {
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        soapMessage.getSOAPHeader().detachNode();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.setAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema");
        envelope.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    } catch(SOAPException e) {
        e.printStackTrace();
    }

Then you can add your elements to the soap body.

    try {

        SOAPBody body = envelope.getBody();
        SOAPElement bodyElement = body.addChildElement(envelope.createName("Connect" , "ns1", ""));                                                      
        SOAPElement findItem = bodyElement.addChildElement("ItemName");
        findItem.setAttribute("xsi:type", "xsd:string");
        findItem.setTextContent("JAX-WS");

    } catch(SOAPException e) {
        e.printStackTrace();
    }

After you are done you can use SOAPConnection to send your message and get the response as a SOAP message.

    try {

        SOAPConnection soapConnection = SOAPConnectionFactory.newInstance().createConnection();
        SOAPMessage returnMessage = soapConnection.call(soapMessage,serverUrl);
    } catch(SOAPException e) {
        e.printStackTrace();
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s