Java Web Services Message Client

Updated Java Web Services Message Client Redux now available.

Most of the Java web services examples on the web use an RPC-style interface. That’s fine when you’re calling a web service implemented in Java. But web services being what they are, you’ll eventually find yourself invoking a message-style web service whose WSDL gives wsdl2java fits.

When that time comes, you can either roll your own code rather than use wsdl2java or you can go with the flow with a message-style interface. I think that you’ll find it easier to go with the message-style interface. And to make it even easier, here is a simple message-style client to get you started.

This client reads an input document from standard input, invokes a web service, and then prints the web service response. The results shown here use Java 1.4.2_11 and Axis 1.3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.constants.Style;
import org.apache.axis.Message;
import org.apache.axis.message.SOAPEnvelope;


public class Simple {

public static void main ( String[] args )
throws Exception
{

String endpoint = "http://stephan-desktop/axis/services/echo";
/*
* If there is a command line argument,
* then use it as the web service endpoint
*/

if ( args.length > 0 ) {
endpoint = args[0];
}
/*
* Read input
*/

String document = streamToString( System.in );
System.out.println( document );
System.out.println( "Invoking " + endpoint + "\n" );
/*
* Pass to web service
*/

Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationStyle( org.apache.axis.constants.Style.MESSAGE );
SOAPEnvelope envelope = call.invoke( new Message(document) );
/*
* Print the Web Service return
*/

System.out.println( envelope.getBody().toString() );
}

/**
* Utility to read textual data from in InputStream to a String
*/

public static String streamToString( java.io.InputStream is )
throws java.io.IOException
{

java.io.InputStreamReader ir = new java.io.InputStreamReader(is);
java.io.BufferedReader in = new java.io.BufferedReader(ir);

StringBuffer buffer = new StringBuffer();
{
String line = null;
while( (line=in.readLine()) != null ) {
buffer.append( line );
buffer.append( '\n' );
}
}
return buffer.toString();
}
}

And here is the client in action, calling the echoString operation of the echo web service example included in the Axis distribution. The actual response body is a single line, I have taken the liberty of formatting the response for easier reading.

1
12 $ javac -classpath $AXISCLASSPATH Simple.java
13 $ java -classpath $AXISCLASSPATH Simple < data/echo.xml
<?xml version="1.0" standalone="yes" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns1="http://soapinterop.org/xsd">
  <soap:Body>
    <ns1:echoString>
      <ns1:echoStringRequest>Hello World</ns1:echoStringRequest>
    </ns1:echoString>
  </soap:Body>
</soap:Envelope>

Invoking http://stephan-desktop/axis/services/echo

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <ns2:echoStringResponse
      soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
      xmlns:ns2="http://soapinterop.org/xsd">
    <return
        xsi:type="xsd:string"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Hello World</return>
  </ns2:echoStringResponse>
</soapenv:Body>
14 $

Feb 9: Examining your Java app with a debugging proxy.
Sep 19: Java Web Services Message Client Redux.