Introduction
Normally calling a SOAP WebAPI requires the developer to access WSDL then based on it generates the stubs linked to a particular server. After generating it, we need to compile those to make them ready to be used. The problem with that one is. Do we use the same server for development, testing and production? If yes, we need to regenerate the stubs for testing and if we are satisfied with the testing. We need to regenerate the stubs for production. Wait? The one tested is not really the one going to production. The simplest way to do is not to generate the stubs but understand how the soap message was structured. If we know the structure of the SOAP message (i.e. SOAP UI can be used here), then we can create it by hand. The following is the sample of assembling a SOAP message for invoking the evaluate method from the StudioService endpoint of PolicyCenter 7 (i.e. One of the Centers of Guidewire) using Java.
Code 1. Using JAXWS API with Java
import java.util.Iterator; public class GWStudioService { public static void main(String[] args) throws SOAPException, IOException { String endpointUrl = "http://localhost:8180/pc/ws/gw/internal/webservice/studio/StudioService"; String targetNamespace = "http://guidewire.com/pl/ws/gw/internal/webservice/studio/StudioService"; QName serviceName = new QName(targetNamespace,"evaluateGosu"); QName portName = new QName(targetNamespace,"StudioServiceSoap12Port"); /** Create a service and add at least one port to it. **/ Service service = Service.create(serviceName); service.addPort(portName, SOAPBinding.SOAP12HTTP_BINDING, endpointUrl); /** Create a Dispatch instance from a service.**/ Dispatch dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); /** Create SOAPMessage request. **/ // compose a request message MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); // Create a message. This example works with the SOAPPART. SOAPMessage request = mf.createMessage(); SOAPPart part = request.getSOAPPart(); // Obtain the SOAPEnvelope and header and body elements. SOAPEnvelope env = part.getEnvelope(); SOAPHeader header = env.getHeader(); SOAPBody body = env.getBody(); String authNS = "http://guidewire.com/ws/soapheaders"; SOAPElement auth = header.addChildElement("authentication", "auth", authNS); SOAPElement username = auth.addChildElement("username", "auth"); username.addTextNode("su"); SOAPElement password = auth.addChildElement("password", "auth"); password.addTextNode("gw"); // Construct the message payload. SOAPElement operation = body.addChildElement("evaluateGosu", "stud", targetNamespace); SOAPElement value = operation.addChildElement("code", "stud"); value.addTextNode("7072696e74282248656c6c6f20776f726c642229"); request.saveChanges(); System.out.println("Request\n"); request.writeTo(System.out); SOAPMessage response = dispatch.invoke(request); System.out.println("\n\nResponse\n"); response.writeTo(System.out); SOAPPart inSOAPPart = response.getSOAPPart(); SOAPEnvelope inEnv = inSOAPPart.getEnvelope(); SOAPBody inBody = inEnv.getBody(); Iterator inGosuResp = inBody.getChildElements(new QName(targetNamespace, "evaluateGosuResponse")); Iterator inReturn = inGosuResp.next().getChildElements(new QName(targetNamespace, "return")); Iterator inEntries= inReturn.next().getChildElements(new QName(targetNamespace, "Entry")); while (inEntries.hasNext()) { SOAPElement inEntry = inEntries.next(); System.out.println(">"); System.out.println(inEntry.getElementName().getLocalName()); System.out.println(inEntry.getTextContent()); } } }
Output 1: Sample output of running code 1
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Header/><soap12:Body><evaluateGosuResponse xmlns="http://guidewire.com/pl/ws/gw/internal/webservice/studio/StudioService"> <return> <Entry>48656C6C6F20776F726C640D0A</Entry> <Entry/> </return> </evaluateGosuResponse></soap12:Body></soap12:Envelope>
To understand the output of the entry we can use the following online Hex to String converter.
https://codebeautify.org/hex-string-converter
Reference:
https://axis.apache.org/axis2/java/core/docs/jaxws-guide.html#DispatchClient
Leave a Reply