Sunday, March 6, 2011

Consume Any Web Service Using Eclipse + Axis

  1. Start Eclipse and create a new workspace named wsTest (click File -> Switch Workspace -> Other…)



  2. Create a new Java Project named wsTest (click File -> New -> Java Project)



  3. 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.



  4. Create a new Java Class named Test.java (click File -> New -> Class)



  5. 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.

  6. 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.

22 comments:

  1. This a very good post and it worked for me. Well done.

    ReplyDelete
  2. WeatherSoapStub ???

    ReplyDelete
  3. simple and working. Thanks

    ReplyDelete
  4. Works great. I am using Eclipse version: "Juno Release"

    ReplyDelete
  5. I'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?

    ReplyDelete
  6. It 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):

    http://www.eclipse.org/downloads/

    ReplyDelete
  7. Good Job. Nice and Simple.

    ReplyDelete
  8. Really Awesome...:)

    ReplyDelete
  9. Followed it through, tried it out, got the overall idea. Thank you.

    ReplyDelete
  10. This a very good post and it worked for me. Well done.

    ReplyDelete
  11. Muy bueno, simple y entendible!

    ReplyDelete
  12. Please Clarify my doubt

    If we change the "webservice url"(service definition) then how to refresh and get the related classes?.

    ReplyDelete
    Replies
    1. Did you ever find out the answer? I want to know how to regenerate the local classes from an updated wsdl.

      Delete
  13. Getting this exception
    heinä 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)

    ReplyDelete
  14. Craig,

    If 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

    ReplyDelete
    Replies
    1. 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:

      try
      {
      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.

      Delete
  15. This a very good post.I still don't get a choice labeled "Web Services." What am I missing?v
    SEO

    ReplyDelete
  16. Hi! Just wanted to let you know that the URL in the code provided in step 5 is misspelled. The 'f' in wsf is missing.

    Otherwise, everything works. Thanks!

    ReplyDelete
  17. Hi, Thank you for this blog, it's very useful.
    Can 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

    ReplyDelete
  18. 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