uCosminexus Application Server, Web Service Development Guide
Create an implementation class for the Web Service client that uses Web Services.
The following is an example of creating a dispatch-based Web Service client com.sample.client.TestClient that invokes a Web Service once.
Note that with a dispatch-based Web Service client, you must explicitly specify the version of the SOAP binding, so the SOAP 1.1/HTTP binding is specified in this example. For SOAP 1.2, substitute SOAPBinding.SOAP11HTTP_BINDING with SOAPBinding.SOAP12HTTP_BINDING.
package com.sample.client;
import java.io.File;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.ws.soap.SOAPBinding;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
public class TestClient {
public static void main( String[] args ) {
// Generate service
QName port = new QName( "http://sample.com", "UserInfoPort" );
Service service = Service.create(
new QName("http://sample.com", "UserInfoService"));
String serviceURL = "http://webhost:8085/dispatch_provider/UserInfoService";
// Add a port to the service
service.addPort( port, SOAPBinding.SOAP11HTTP_BINDING, serviceURL );
// Generate Dispatch object
Dispatch<SOAPMessage> dispatch = service.createDispatch(
port, SOAPMessage.class, Service.Mode.MESSAGE );
// Request message
SOAPMessage request = null;
try{
// Generate request message
request = MessageFactory.newInstance().createMessage();
SOAPBody reqSoapBody = request.getSOAPBody();
// Set up employee number
SOAPBodyElement requestRoot= soapBody.addBodyElement(
new QName("http://sample.com", "number"));
SOAPElement soapElement = requestRoot.addChildElement(
new QName("http://sample.com", "value"));
soapElement.addTextNode( "1234" );
// Set up attachment (face photograph)
String filePath = "C:\\attachment.jpg";
FileDataSource fds = new FileDataSource(filePath);
AttachmentPart apPart =
request.createAttachmentPart(new DataHandler(fds));
request.addAttachmentPart(apPart);
// Sending and receiving SOAP Messages
SOAPMessage response = dispatch.invoke( request );
// Acquire data from the response message
SOAPBody resSoapBody = response.getSOAPBody();
SOAPBodyElement resRoot =
(SOAPBodyElement)resSoapBody.getChildElements().next();
Iterator iterator = resRoot.getChildElements();
String result =
((SOAPElement)iterator.next()).getFirstChild().getNodeValue();
// Display the registration confirmation message
System.out.println( "[RESULT] " + result );
} catch( SOAPException e ) {
e.printStackTrace();
}
}
}
|
The created class TestClient.java is saved in the c:\temp\jaxws\works\dispatch_provider\client\src\com\sample\client\ directory with the UTF-8 format.
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.