|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes how to consume Web services in Java 1.5.0 using the new JAX-WS 2.0 API (JSR 228). Developers around the world, including me, have always complained about the hard ways to work in Java to consume even a Web service as simple as adding two numbers. However, with JAX-WS 2.0 API now available in core Java in JDK 1.5.0, life is simple like never before. The article describes how this API can be used for maximum benefits using some off-the-shelf tools similar to wsdl.exe available from Microsoft in the .Net Framework to generate the proxy classes. Before starting with the discussion, some vocabulary background may be needed for first timers. Following are simple definitions to the terms used:
Publishing a Web Service using ASP.NetFor our case, we would publish a Web service using ASP.NET. You can look into other articles around the globe onto how to publish a Web service using ASP.NET. Here is the synopsis of the Web service: Name: BasicWebServices
Namespace: http://www.edujinionline.com/ns/ws/samples/dn/S01BasicServices/
Operation: Add
Input Message: AddMessage
Output Message: AddMessageResponse
AddMessage: Two parameters, x and y, of type double
AddMessageResponse: One parameter, AddMessageResult, of type double
Consuming the Web Service using JAX-WS 2.0 in Java 1.5Download JAX-WS Reference Implementation from here. Extract the files using the procedure described in the installation section of the document above. Dig into the bin folder that would have a utility called wsimport. Yes! All those who have worked with wsdl.exe in the .NET Framework would immediately understand what the purpose of the tool is. Apart from various other options, the important options supported by the tool are:
On the console, issue the following command (all in one line): wsimport
–p com.edujinionline.tutorials.services
–s src
–d bin
http://localhost/BasicWebServices.asmx?WSDL
The last parameter is the location of the WSDL file. Yes, it works with HTTP transport beautifully. Now… now, what? The proxy class is available. You will find the following classes generated:
... and these are the proxy classes ready for use. See the code below for demonstration. public class BasicWebServicesClient
{
public static double testAdd(double x, double y)
{
double retVal = 0;
BasicWebServices service = new BasicWebServices();
BasicWebServicesSoap soap = service.getBasicWebServicesSoap();
ObjectFactory factory = new ObjectFactory();
AddMessage request = factory.createAddMessage();
request.setX(x);
request.setY(y);
AddMessageResponse response = soap.add(request);
retVal = response.getAddMessageResult();
return retVal;
}
}
And the main method to use the wrapper method public class MainClass
{
public static void main(String[] args)
{
double x = 34.56;
double y = 45.67;
double z = BasicWebServicesClient.testAdd(x, y);
System.out.printf("%f + %f = %f", x, y, z);
}
}
SummaryConsuming Web services in Java requires just two steps now. The first step is to create proxy classes. The second step is to go ahead and use them. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||