Calling webservice from Java

Is there a way to call a webservice trough a java action, and passing it the full message, inclusive of soap envelope definition?
3 answers

Yes this is possible, you could use an external library like JAX-WS or SAAJ. Why would you need to build in in Java, doesn't the service work with the built in Soap functionality?


This is true Simon, but I need to be able to add additional data in the soap header itself, the header now render as the following

<soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi="

but it need to look like this:

<soapenv:Envelope xmlns:soapenv="" xmlns:oas="" xmlns:v2="" xmlns:v1="">

then the attributes v1 and v2 need to be used as a reference, in the body part of the message, for example

                <v2:name>Mr Test test</v2:name>
                <v2:complementaryName>test 98</v2:complementaryName>
                <addressLine1>69 London Road</addressLine1>
                <postcode>RG1 3AQ</postcode>

Call a webservice from java snippet. It contains specific code but it could help you

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;


            try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            InputStream is = null;
            if (cacheType == CacheType.MEMORY) {
                is = new ByteArrayInputStream(this.request.getBytes());
            if (cacheType == CacheType.FILE) {
      "read from file");
                is = new FileInputStream(this.fileName);
            // copy original message into this.
            SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(null, is);                 
            String url = "" + endpoint;
            splogger.trace("invoke url " + url);
            SOAPMessage soapResponse =, url);
            SOAPBody soapBody = soapResponse.getSOAPBody();
// Adapt processing to your response
            NodeList faultList = soapBody.getElementsByTagName("faultcode");
            NodeList resultList = soapBody.getElementsByTagName("Result");
            // default value;
            Boolean resultValue = true;
            if (resultList.getLength() > 0) {
                Element value = (Element) resultList.item(0);
                if ((value.getTextContent() != null) && (value.getTextContent().equals("false"))) {
                    resultValue = false;

        } catch (Exception e) {
            splogger.error("Error occurred while sending SOAP Request to Server", e);
        splogger.trace("webservice called");