devnumbertwo - $h!t for developers

View Original

create a java web service client from a wsdl

you want to create a java client from a wsdl online?

boom! here you go:

  1. Ensure you have the JDK installed

  2. Create a new eclipse java project

  3. Add wsimport (located in the JDK bin) to your path
    Example:

    set PATH=%PATH%;C:\Program Files\Java\jdk1.6.0_41\bin
  4. Use wsimport to generate the Java files from the WSDL
    Change directory to your Eclipse project directory, run wsimport:

    C:\Users\devnumbertwo\Desktop\Projects\workspace\WSClient>wsimport -s src -d bin http://devnumbertwo.com/webservice-server/server/soap?wsdl
    parsing WSDL...
    
    generating code...
    
    compiling code...
    
    C:\Users\devnumbertwo\Desktop\Projects\workspace\WSClient>

  5. Refresh your Eclipse project then create a class file using the generated code to hit the web service. Something like:

    public class MainContainer {
    
    	public static void main(String args[]) {
    		MainContainer main = new MainContainer();
    			main.run();
    	}
    
    	public void run() {
    		System.out.println("--- start ----");
    
    		WSServer os = new WSServer();
    		WSServerType osType = os.getWSServer();
    
    		// ======= Get Server Info =======
    		GetServerInfoRequest srvrInfoReq = new GetServerInfoRequest();
    
    		GetServerInfoResponse srvrInfoResp = osType.getServerInfo(srvrInfoReq);
    
    		String timez = srvrInfoResp.getWSEngineTimezone();
    		String engVersion = srvrInfoResp.getWSEngineVersion();
    		String srvrVersion = srvrInfoResp.getWSServerVersion();
    
    		StringBuffer strPrint = new StringBuffer("Listing Server Info: \n");
    		strPrint.append("\tTimezone: " + timez + "\n");
    		strPrint.append("\tEngine Version: " + engVersion + "\n");
    		strPrint.append("\tServer Version: " + srvrVersion + "\n");
    
    		System.out.println(strPrint.toString());
    
    	}
    }