Calling webservice from Java

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

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?

answered
0

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="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

but it need to look like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:v2="http://www.royalmailgroup.com/api/ship/V2" xmlns:v1="http://www.royalmailgroup.com/integration/core/V1">

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

<soapenv:Body>
    <v2:createShipmentRequest>
        <v2:integrationHeader>
            <v1:dateTime>2016-02-25T08:52:03</v1:dateTime>
            <v1:version>2</v1:version>
            <v1:identification>
                <v1:applicationId>0021382001</v1:applicationId>
                <v1:transactionId>730222611</v1:transactionId>
            </v1:identification>
        </v2:integrationHeader>
        <v2:requestedShipment>
            <v2:shipmentType>
                <code>Delivery</code>
            </v2:shipmentType>
            <v2:serviceOccurrence>1</v2:serviceOccurrence>
            <v2:serviceType>
                <code>1</code>
            </v2:serviceType>
            <v2:serviceOffering>
                <serviceOfferingCode>
                    <code>PK3</code>
                </serviceOfferingCode>
            </v2:serviceOffering>
            <v2:serviceFormat>
                <serviceFormatCode>
                    <code>F</code>
                </serviceFormatCode>
            </v2:serviceFormat>
            <v2:shippingDate>2016-02-25</v2:shippingDate>
            <v2:recipientContact>
                <v2:name>Mr Test test</v2:name>
                <v2:complementaryName>test 98</v2:complementaryName>
                <v2:telephoneNumber>
                    <countryCode>001</countryCode>
                    <telephoneNumber>07801123456</telephoneNumber>
                </v2:telephoneNumber>
                <v2:electronicAddress>
                    <electronicAddress>test.smith@test.com</electronicAddress>
                </v2:electronicAddress>
            </v2:recipientContact>
            <v2:recipientAddress> 
                <addressLine1>69 London Road</addressLine1>
                <postTown>Reading</postTown>
                <postcode>RG1 3AQ</postcode>
                <country>
                    <countryCode>
                        <code>GB</code>
                    </countryCode>
                </country>
            </v2:recipientAddress>
            <v2:items>
                <v2:item>
                    <v2:numberOfItems>5</v2:numberOfItems>
                    <v2:weight>
                        <unitOfMeasure>
                            <unitOfMeasureCode>
                                <code>g</code>
                            </unitOfMeasureCode>
                        </unitOfMeasure>
                        <value>100</value>
                    </v2:weight>                                                                                    
                </v2:item>
            </v2:items>
            <v2:customerReference>CustSuppRef1</v2:customerReference>
            <v2:senderReference>SenderReference1</v2:senderReference>
        </v2:requestedShipment>
    </v2:createShipmentRequest>
</soapenv:Body>
answered
0

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;

snippet

            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) {
                splogger.info("read from file");
                is = new FileInputStream(this.fileName);
            } 
            // copy original message into this.
            SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(null, is);                 
            String url = "http://some.site.com/" + endpoint;
            splogger.trace("invoke url " + url);
            SOAPMessage soapResponse = soapConnection.call(soapMessage, 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;
                }
            }

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