2011年10月12日 星期三

[Java] Creating a Web Service Client in Eclipse

Step-by-step:
  1. New a Java Project, say WebServiceClientDemo, in Eclipse.
  2. Select WebServiceClientDemo project, right click > New > Other... > Web Services > Web Service Client. Click next. 
  3. Type http://localhost:5566/WebServiceDemo/services/HelloWorld?wsdl in service definition. Click finish. Eclipse will generate several Java classes for us. For example, the code of HelloWorld.java is
    /**
     * HelloWorld.java
     *
     * This file was auto-generated from WSDL
     * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
     */
    
    package demo;
    
    public interface HelloWorld extends java.rmi.Remote {
        public java.lang.String greeting(java.lang.String in0) throws java.rmi.RemoteException;
    }
    
  4. New a Java class to call greeting() method - Main.java:
    package demo;
    
    public class Main {
        public static void main(String[] args) {
            try {
                HelloWorldService service = new HelloWorldServiceLocator();
                HelloWorld helloWorld = service.getHelloWorld();
                System.out.println(helloWorld.greeting("Plover"));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  5. Run as Java Application. We should see the following message in Console:
    Hello World! Plover!
    
Enjoy!

沒有留言:

張貼留言