views:

216

answers:

1

Hi, I am new to webservices in general. I am trying to write a Java stand-alone client which can get a response back from a webservice.

I tried searching SO and Google but now I got more confused. The below are the links I went through extensively.

I have a url like: http://api.something.com/remote/wsdl/SomeEncryptedText

I also have a SOAP request something like:

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"&gt; 
<soap12:Body> 
<AuthUsername>[email protected]</AuthUsername> 
<AuthPassword>mypassword</AuthPassword> 
<Sid>12121</Sid> 
<DynamicProductFeedsRequest xmlns="http://api.something.com/remote/SomeEncryptedText"&gt; 
</DynamicProductFeedsRequest> 
</soap12:Body> 
</soap12:Envelope> 

With this how do I write a stand-alone Java client which I would want to integrate with some web application at a later stage?

From the resources mentioned earlier looks there is a wide choice of softwares: SoapUI, WSDL2Java, Apache Axis, Maven Plugin, JAX-WS, Apache CXF.

I used http://www.soapclient.com/soaptest.html in one of the SO answers mentioned above and I am able to get a perfect html/xml file on the browser.

Now I am confused on which is the software I should use? The information in the links are little in bits and pieces which I am unable to correlate with one another since I do not know anything in SOA.

Could anyone please tell me the high level steps in writing a stand-alone Java client which takes in the WSDL URL and SOAP request and gives me the output of it?

Please let me know if I missed any information.

+3  A: 

This question all depends on the following:

  • The JDK version of your Java compiler.
  • Your WSDL version (there's 1.0, 1.2 and 2.0).

Basically, if you are using Java annotations to generate web services, then you'll need Java 5 related Web Services libraries (which supports annotations).

Some articles on Using Java Web Services with annotations (JAX-WS):

I'll start from generating Web Service client with Java that doesn't support annoations. The well known client that generates WSDL to java is Apache Axis (the last version is 1.4 released in 22 April 2006). This basically takes a WSDL definition and generates it back to client. It supports the old version of WSDL (1.0) and crashes if you use the newer versions of WSDL (1.2 and 2.0).

What this basically does, it takes your WSDL and generates a java Proxy that communicates to your Web Service. It can allow RPC based as well as XML based communication.

For Java that supports annotations there are, effectively, 2 ways of doing this:

  • Using Java's own wsimport command (the executable is found under the JDK_HOME/bin/ folder).
  • Using 3rd Party libaries such as Apache Axis 2 (which effectively replaces Apache Axis and supports WSDL version 2.0) or Apache CXF (which supports WSDL up to 1.2).

To use wsimport, you basically need to go to a shell command (or write a script) and effectively do something of this effect:

wsimport -d [outputdir] wsdl_file

and your java proxy will be found in the [outputdir] folder.

wsimport is found in JDK 1.6 (I don't know if it exists in earlier versions). More source here, and here.

For Apche Axis, Apache Axis 2 or Apache CXF, there's a WSDL2Java class file that does source code generation.

Here's a guide on how to use WSDL2Java in Apache CXF and in Apache Axis 2.

I hope this helps you in some way as much as I spent like 30 minutes off work doing this. :-)

The Elite Gentleman
Hi, Thanks a lot. Just in the order I am exploring JoseK's solution. I had the issue with WSDL2Java which gave me the below error;> WSDLToJava Error: Rpc/encoded wsdls are not supported with CXFFixed it using a solution from SO.I basically removed the encoding and now generated two files 1. RemoteModelDynamicProductFeedsPort.java and 2. RemoteModelDynamicProductFeedsService.java. Need to proceed with steps in the link.I will also try "The Elite Gentleman's" post when stuck. Thanks! That was in great detail.
oneworld
@Gentleman, just call it documenting existing practices and saving it at an off-site location, and your boss will ask you to do it again.
Thorbjørn Ravn Andersen
I used the CFX tutorial got three files generated as below:RemoteModelDynamicProductFeedsPort.javaRemoteModelDynamicProductFeedsPort_RemoteModelDynamicProductFeedsPort_Client.javaRemoteModelDynamicProductFeedsService.javaAnd then issued:wsdl2java -ant -client -d D:\experiementals\webservice-java\apache-cxf-2.2.10\bin -b async_binding.xml wsdlfile.wsdl Nothing happens. I thought I have generated the stubs and also have passed the SOAP request. Any thoughts on this? Basically the tutorial is dealing with some complex stuffs. Is it because that I removed encryption, it does nothing?
oneworld
hi, could anybody help me. Think there are lot of errors in the files generated by CFX. There is a mismatch of the class name mentioned and the file name created. The same is also the case with the classes being called in one another Ex: RemoteModelDynamicProductFeedsPort_RemoteModelDynamicProductFeedsPort_Client.java and the class is declared as RemoteModelDynamicProductFeedsPort. Also there are underscores between each word in the files where the programs are invoked but the file name is without underscores.
oneworld
The Elite Gentleman
I did the following: 1. wsdl2java -ant -client -d D:\exp\webservice-java\apache-cxf-2.2.10 D:\exp\webservice-java\aWsdlFile.wsdl and got the below three '.java' files 2. went to the path D:\exp\webservice-java\apache-cxf-2.2.10\https\api_hl_com\remote\_3ceeabb066b6d76a1eeb57348767a95a and compiled the java files <javac *.java> and got the 3 '.class' files3. Now setting the classpath and running the main java program as below: (but I get errors)
oneworld
This is the error: D:\exp\webservice-java\apache-cxf-2.2.10\https\api_hl_com\remote\_3ceeabb066b6d76a1eeb57348767a95a>java -cp D:\exp\webservice-java\apache-cxf-2.2.10 https.api_hl_com.remote._3ceeabb066b6d76a1eeb57348767a95a.RemoteModelDynamicProductFeedsPort_RemoteModelDynamicProductFeedsPort_Client Invoking dynamicProductFeedsRequest... Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.net.ConnectException: Connection timed out: connect
oneworld
It's a connection exception. Web Services run on a Server (hence the word Web in front of Services). Run the Web Services from the Web server **and** generate the client from the WSDL deployed in the Web Server.
The Elite Gentleman
Thanks a lot "The Elite Gentleman". The issue was due to proxy setting. I placed the below lines of code into RemoteModelDynamicProductFeedsPort_Client.java and RemoteModelDynamicProductFeedsService.javaSystem.setProperty("http.proxyHost","proxy.mycompany.com");System.setProperty("http.proxyPort","8080"); System.setProperty("https.proxyHost","proxy.mycompany.com");System.setProperty("https.proxyPort","8080");
oneworld
Also I set the java.lang.String _dynamicProductFeedsRequest_xml variable in RemoteModelDynamicProductFeedsPort_Client.java with the SOAP request (ie., XML string). It gets the result atleast partially. I need to check further about the issue. I really appreciate your patience. Thanks.
oneworld
No problems....
The Elite Gentleman