SOAP Client in java
original link
October 20, 2011
This article will teach you how to create a SOAP client in java. That is creating a client in java which requests
soap server (
no need to be in java) and get response from it. First create request message as follows:
1 | SOAPMessage message = MessageFactory.newInstance().createMessage(); |
2 | SOAPHeader header = message.getSOAPHeader(); |
If you have namespace required in SOAP request then add those namespaces to envelope element as follows:
1 | SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); |
2 | envelope.setAttribute( "namspace" , "namespaceUrl" ); |
You can add as many attribute as you want. Now time to create request message body.
Following body is made assuming that SOAP server where this client will
connect will have a public service method called getResponse(name)
available.
1 | SOAPBody body = message.getSOAPBody(); |
2 | QName bodyName = new QName( "getResponse" ); |
3 | SOAPBodyElement bodyElement = body.addBodyElement(bodyName); |
4 | SOAPElement symbol = bodyElement.addChildElement( "name" ); |
5 | symbol.addTextNode(“Harry joy”); |
Now that request message is ready it’s time to connect to soap server and send request. Following code will do this:
1 | SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); |
2 | SOAPMessage response = connection.call(message, endpoint); |
In above code endpoint is the SOAP server URL without “?wsdl”. To parse response you can do as follows:
1 | SOAPBody responseBody = response.getSOAPBody(); |
2 | SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next(); |
3 | SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next(); |
4 | if (responseBody.getFault() != null ) { |
5 | System.out.println(returnElement.getValue()+ " " +responseBody.getFault().getFaultString()); |
7 | System.out.println(returnElement.getValue()); |
Here request/response messages are totally dependent on SOAP server, how you have configured it.
Bonus: How to print request/response xml?
1 | String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException { |
2 | ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); |
3 | msg.writeTo(byteArrayOS); |
4 | return new String(byteArrayOS.toByteArray()); |
Use:
1 | System.out.println(getXmlFromSOAPMessage(message)); |
2 | System.out.println(getXmlFromSOAPMessage(response)); |
Full source code:
public class SOAPClient {
private static final String endpoint = "http://localhost/SOAPService/MySoapService";
public static void main(String[] args) throws SOAPException {
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPHeader header = message.getSOAPHeader();
header.detachNode();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.setAttribute("namespace","namespaceUrl");
SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("getResponse");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
SOAPElement symbol = bodyElement.addChildElement("name");
symbol.addTextNode("Harry Joy");
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();
SOAPBody responseBody = response.getSOAPBody();
SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
if(responseBody.getFault()!=null){
System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
} else {
System.out.println(returnElement.getValue());
}
try {
System.out.println(getXmlFromSOAPMessage(message));
System.out.println(getXmlFromSOAPMessage(response));
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
msg.writeTo(byteArrayOS);
return new String(byteArrayOS.toByteArray());
}
}
No comments:
Post a Comment