-
Start Eclipse and create a new workspace named wsTest (click File -> Switch Workspace -> Other…)
-
Create a new Java Project named wsTest (click File -> New -> Java Project)
-
Create a new Web Service Client Proxy (click File -> New -> Other -> Web Services -> Web Service Client). In this case we are consuming a free, public web service that returns weather information for the United States. The URL for the WSDL definition is http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl.
-
Create a new Java Class named Test.java (click File -> New -> Class)
-
Copy and paste the following code into Test.java (overwrite the existing code):
import com.cdyne.ws.WeatherWS.*; import java.net.URL; public class Test { public static void main(String[] arg) { try { WeatherSoapStub service = (WeatherSoapStub)new WeatherLocator().getWeatherSoap(new URL("http://ws.cdyne.com/WeatherWS/Weather.asmx")); WeatherReturn weather = service.getCityWeatherByZIP("47710"); System.out.println("Location: " + weather.getCity() + ", " + weather.getState()); System.out.println("Description: " + weather.getDescription()); System.out.println("Temperature: " + weather.getTemperature() + " degrees"); } catch (Exception e) { e.printStackTrace(); } } }
Note that we set the endpoint URL on line 10. The endpoint URL is typically the same as the WSDL definition URL except the endpoint URL does not include the ?WSDL query string parameter.
-
Execute the code (click Run -> Run)
You should see something resembling the following output:
Location: Evansville, IN
Description: Sunny
Temperature: 49 degrees
At the time of writing this post I am using Eclipse Helios with the J2EE extensions installed; options may vary slightly if using a different version.
How It Works
Eclipse is executing the Axis Wsdl2Java process which parses the WSDL file and translates it into a set of Java client files that you can then use in your own application. It is possible to accomplish the same thing outside of Eclipse by directly executing the Axis Wsdl2Java process yourself.
This a very good post and it worked for me. Well done.
ReplyDeleteThank you :)
ReplyDeleteWeatherSoapStub ???
ReplyDeletesimple and working. Thanks
ReplyDeleteWorks great. I am using Eclipse version: "Juno Release"
ReplyDeleteI've put the Axis plugin into my Eclipse plugins directory, but when I go File | New | Other, I still don't get a choice labeled "Web Services." What am I missing?
ReplyDeleteIt sounds like you are missing the WTP (Web Tools Platform) plugin. I'm sure you can manually install the WTP plugin. I always download the Java EE version of Eclipse which comes pre-packaged with these plugins (top link):
ReplyDeletehttp://www.eclipse.org/downloads/
Good Job. Nice and Simple.
ReplyDeleteReally Awesome...:)
ReplyDeleteFollowed it through, tried it out, got the overall idea. Thank you.
ReplyDeleteThis a very good post and it worked for me. Well done.
ReplyDeleteMuy bueno, simple y entendible!
ReplyDeletePlease Clarify my doubt
ReplyDeleteIf we change the "webservice url"(service definition) then how to refresh and get the related classes?.
Did you ever find out the answer? I want to know how to regenerate the local classes from an updated wsdl.
DeleteGetting this exception
ReplyDeleteheinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE: Usage: Admin client|server
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE: Where looks like:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
heinä 30, 2013 6:23:50 IP. org.apache.axis.utils.Admin main
SEVERE:
Exception in thread "main" java.lang.IllegalArgumentException: Usage: Admin client|server
at org.apache.axis.utils.Admin.main(Admin.java:277)
Craig,
ReplyDeleteIf I need to setup a weather web service to get daily weather data by station (there are a total of 9 stations I need). How do I get the application to write to a file (i.e. CSV or TXT file) instead of displaying a web page with the data? I need to be able to map the file in an ODI mapping to a DB. I've searched and searched and can't seem to find anything that gives detail on how to write to a file with a web service application.
Thanks so much in advance!
Collette
Collette, I'm not sure what an ODI mapping is, but writing the data to a file is relatively simple. For starters you can try using the java.io.PrintWriter class. It works very similar to System.out.println. Below is a short example:
Deletetry
{
java.io.PrintWriter pw = new java.io.PrintWriter("c:\\test.txt");
pw.println("Location: " + weather.getCity() + ", " + weather.getState());
pw.println("Description: " + weather.getDescription());
pw.println("Temperature: " + weather.getTemperature() + " degrees");
pw.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
I've not tested this code but it should be pretty close.
This a very good post.I still don't get a choice labeled "Web Services." What am I missing?v
ReplyDeleteSEO
Excellent worked for me!
ReplyDeleteHi! Just wanted to let you know that the URL in the code provided in step 5 is misspelled. The 'f' in wsf is missing.
ReplyDeleteOtherwise, everything works. Thanks!
Hi, Thank you for this blog, it's very useful.
ReplyDeleteCan you help me? I need to add a user and password to authenticate when consuming the WebService. How can I do that?
It's returning the error AxisFault (401)Unauthorized
Texting is the go-to communication tool for everyone today even with the emergence of social media. This is why businesses use it to communicate with prospects or customers in order to ensure that information is received and responded to. Plus, with new tech every day, they can now connect their system with a texting service using a Tex API to send messages directly as well.
ReplyDelete