By Marone: July 2015
Consuming web service
Goal
This article will show you how to create a dynamic soap web service client with dispatch API.Used technologies
JDK 1.7Using SOAPMessage
package com.wstutorial.ws.test;
import java.io.*;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.*;
public class WSClientTest {
public static void main(String[] args) throws IOException, SOAPException {
WSClientTest client = new WSClientTest();
client.testWithSAOPMessage();
}
public void testWithSAOPMessage() throws IOException, SOAPException {
QName serviceName = new QName("http://ws.wstutorial.com/", "HelloWorldImplService");
URL wsdlURL = new URL("http://localhost:8090/ws/helloworld?wsdl");
Service jaxwsService = Service.create(wsdlURL, serviceName);
QName portName = new QName("http://ws.wstutorial.com/", "HelloWorldImplPort");
Dispatch
disp =
jaxwsService.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
InputStream is = new FileInputStream("files/soapmessage.xml");
SOAPMessage reqMsg = MessageFactory.newInstance().createMessage(null, is);
SOAPMessage response = disp.invoke(reqMsg);
SOAPBody soapBody = response.getSOAPBody();
System.out.println(soapBody.getTextContent());
}
}
files/soapmessage.xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ws="http://ws.wstutorial.com/">
<soapenv:Header/>
<soapenv:Body>
<ws:sayHelloWorld>
<arg0>Bonjour</arg0>
</ws:sayHelloWorld>
</soapenv:Body>
</soapenv:Envelope>
Output
Hello Bonjour !