Tuesday, April 7, 2015

Creating SOAP client

1. create simple maven project.
You can create directly if you use STS(Spring Tool Suite), Eclipse, you have maven plugin , or directly create using maven tool.

2. Now you need to create subs for the client. To access the webservice.
We will use the web service which was taken from the mkyong.
http://www.mkyong.com/webservices/jax-ws/jax-ws-java-web-application-integration-example/

This has some issues, missing jaxws-rt.jar etc. I have explained what you should do in
http://cgenit.blogspot.com/2015/04/error-deploying-soap-application.html

best thing is to add jaxws-rt.jar as maven dependency. you can get the maven dependency by simply doing a google search for maven dependency.

let's get back  into building client stubs

3. Now you need apache cxf libraries to build the stubs. These libs will give you access for specific set of commands where you can use for various purposes.
You can donload this from apache cxf site.
http://cxf.apache.org/docs/tools.html
here i will be using wsdl2java , where this will help me to build the stubs.
I have downloaded whole set. you can unzip where ever you need.
I have unzip it to the "C:\Softwares\Apache" folder
so my location for wsdl2java will looks like "C:\Softwares\Apache\apache-cxf-3.0.4\bin"
this will have lots of

Next use of the command "wsdl2java"

4. set path to "wsdl2java"  tool
take command prompt.
you get the command prompt from the programs menu in windows or simply get the run (windows + R) , then type "cmd" and press enter.

type "wsdl2java" in the command prompt.
It will give the message
=================================================================
'wsdl2java' is not recognized as an internal or external command,
operable program or batch file.
=================================================================

you need to set the path to the commands.

below is the commad

===================================================================
>set path=C:\Softwares\Apache\apache-cxf-3.0.4\bin
===================================================================

bin folder is the directory, where you have all the apache tools including "wsdl2java"

Now if you run the same command, you will get the output like as below.
this is same as setting path to java bin

note : i am executing the command from respective to the directory i have created
"C:\waste\webservices\client\WebClientTest>" and it will prompt straight away for missing argument list.

I have create "WebClientTest" directory to include the created stubs from "wsdl2java"


===================================================================

C:\waste\webservices\client\WebClientTest>wsdl2java
Missing argument: wsdlurl

Usage : wsdl2java -fe|-frontend -db|-databinding -wv -p <[wsdl-namespace =]package-name>
* -sn -b * -reserveClass * -catalog -d -compile -classd
ir -impl -server -client -clientjar -all -autoNameResolution -allowElementReferences|-aer<=true>
 -defaultValues<=class-name-for-DefaultValueProvider> -ant -nexclude * -exsh <(true, false)> -noType
s -dns -dex <(true, false)> -validate<[=all|basic|none]> -keep -wsdlLocation -xjc* -as
yncMethods<[=method1,method2,...]>* -bareMethods<[=method1,method2,...]>* -mimeMethods<[=method1,method2,...]>* -noAddressBinding -faultSeri
alVersionUID -encoding -exceptionSuper -mark-generated -h|-?|-help -version|-v -verbose
|-V -quiet|-q|-Q -wsdlList


WSDLToJava Error: org.apache.cxf.tools.common.toolspec.parser.BadUsageException: Missing argument: wsdlurl



C:\waste\webservices\client\WebClientTest>

===================================================================



5. create stubs

run the command "wsdl2java -client http://localhost:8080/webservices/hello?wsdl"

wsdl2java   - is the tool from apache cxf
-clent -  choice given by apache tool wsdl2java to create client stubs
http://localhost:8080/webservices/hello?wsdl  - is the location for wsdl, here i have used locally developed and deployed service in the tomcat, you can give any location to the wsdl file

Note: I have not given the output directory for client stubs. stubs will be created in the directory where you are running the command.
In my case it will be "C:\waste\webservices\client\WebClientTest"

So my command and output will be
Note: empty command return after execution of the command.
This will create structure for stubs inside "C:\waste\webservices\client\WebClientTest" directory.

===================================================================
C:\waste\webservices\client\WebClientTest>wsdl2java -client http://localhost:8080/webservices/hello?wsdl

C:\waste\webservices\client\WebClientTest>

===================================================================



6. Copy paste the created directory sructure to the src folder inside your maven project.
Normally it has "src\main\java" folder structure.
I have created the maven project with "WebClientTest" , so the structure would be "C:\eclipseWorkSpace\2015-01-19_SpringSecurity\WebClientTest\src\main\java"

copy the stubs to "C:\eclipseWorkSpace\2015-01-19_SpringSecurity\WebClientTest\src\main\java"

Note : stubs will have below folder structure
"C:\waste\webservices\client\WebClientTest\com\mkyong\ws"

where "C:\waste\webservices\client\WebClientTest\"  is the created folder for include stubs file by running "wsdl2java" command
It will create stubs inside folder / or package structure "com\mkyong\ws" , this is the webservice code structure we have deployed in the tomcat.

structure would be as below , where you will have 7 java files.
==============================================================

C:\waste\webservices\client\WebClientTest\com\mkyong\ws>dir
 Volume in drive C is Windows
 Volume Serial Number is 76A1-C302

 Directory of C:\waste\webservices\client\WebClientTest\com\mkyong\ws

04/07/2015  03:33 PM              .
04/07/2015  03:33 PM              ..
04/07/2015  03:33 PM             1,398 GetHelloWorld.java
04/07/2015  03:33 PM             1,532 GetHelloWorldResponse.java
04/07/2015  03:33 PM             1,219 HelloWorld.java
04/07/2015  03:33 PM             3,417 HelloWorldService.java
04/07/2015  03:33 PM             1,934 HelloWorld_HelloWorldPort_Client.java
04/07/2015  03:33 PM             2,488 ObjectFactory.java
04/07/2015  03:33 PM                99 package-info.java
               7 File(s)         12,087 bytes
               2 Dir(s)  386,769,772,544 bytes free

===================================================================

7. Write main class / client  to test the created client and web service.

create "ClientHello.java" class under package "com.webservices.client".
We write the main method inside here.

* first we create service object. Stubs will have a class with name "service". In this case it is "HelloWorldService" , this is an extension of "Service" provided by "javax.xml.ws" inside the "rt.jar"

                // create service
HelloWorldService helloWorldService =  new HelloWorldService();

* Then we create the port object
                // create soap object from service which is port. 
HelloWorld helloWorld = helloWorldService.getHelloWorldPort();

* once we create the port, we access the web service methods via the port. This time we have only one.
               // through the port we call the methods.
String output = helloWorld.getHelloWorld("John");

* you ill see the out put as
                     Hello World JAX-WS John

Below is the main method

ClientHello.java
=-==================================================================
package com.webservices.client;

import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.HelloWorldService;

public class ClientHello {
public static void main(String[] args) {

// create service
HelloWorldService helloWorldService =  new HelloWorldService();
// create soap object from service which is port.
HelloWorld helloWorld = helloWorldService.getHelloWorldPort();

// through the port we call the methods.
String output = helloWorld.getHelloWorld("John");

System.out.println(output);
}
}

=-==================================================================



8. wsdl2java generates test client itself to test the generated stubs and webservice.
In this case you have seen java class with the name "HelloWorld_HelloWorldPort_Client.java"
This has the main method which has been design to test the webservice

code will looks like below

HelloWorld_HelloWorldPort_Client.java
=-==================================================================

package com.mkyong.ws;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.0.4
 * 2015-04-07T15:33:01.951+05:30
 * Generated source version: 3.0.4
 *
 */
public final class HelloWorld_HelloWorldPort_Client {

    private static final QName SERVICE_NAME = new QName("http://ws.mkyong.com/", "HelloWorldService");

    private HelloWorld_HelloWorldPort_Client() {
    }

    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = HelloWorldService.WSDL_LOCATION;
        if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
            File wsdlFile = new File(args[0]);
            try {
                if (wsdlFile.exists()) {
                    wsdlURL = wsdlFile.toURI().toURL();
                } else {
                    wsdlURL = new URL(args[0]);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
     
        HelloWorldService ss = new HelloWorldService(wsdlURL, SERVICE_NAME);
        HelloWorld port = ss.getHelloWorldPort();
       
        {
        System.out.println("Invoking getHelloWorld...");
        java.lang.String _getHelloWorld_arg0 = "";
        java.lang.String _getHelloWorld__return = port.getHelloWorld(_getHelloWorld_arg0);
        System.out.println("getHelloWorld.result=" + _getHelloWorld__return);


        }

        System.exit(0);
    }

}

=-==================================================================


output will be as below
=-==================================================================

Invoking getHelloWorld...
getHelloWorld.result=Hello World JAX-WS
=-==================================================================


Note : we have invoked the "getHelloWorld" method with empty string. Default values has been set to empty string in the creation time from the "wsdl2java"

so if you assign a value to "_getHelloWorld_arg0" it will have output with the given value.
let's assign "John"

the new code and value would be as below.


HelloWorld_HelloWorldPort_Client.java
=-==================================================================


package com.mkyong.ws;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.0.4
 * 2015-04-07T15:33:01.951+05:30
 * Generated source version: 3.0.4
 *
 */
public final class HelloWorld_HelloWorldPort_Client {

    private static final QName SERVICE_NAME = new QName("http://ws.mkyong.com/", "HelloWorldService");

    private HelloWorld_HelloWorldPort_Client() {
    }

    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = HelloWorldService.WSDL_LOCATION;
        if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
            File wsdlFile = new File(args[0]);
            try {
                if (wsdlFile.exists()) {
                    wsdlURL = wsdlFile.toURI().toURL();
                } else {
                    wsdlURL = new URL(args[0]);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
     
        HelloWorldService ss = new HelloWorldService(wsdlURL, SERVICE_NAME);
        HelloWorld port = ss.getHelloWorldPort();
       
        {
        System.out.println("Invoking getHelloWorld...");
        java.lang.String _getHelloWorld_arg0 = "John";
        java.lang.String _getHelloWorld__return = port.getHelloWorld(_getHelloWorld_arg0);
        System.out.println("getHelloWorld.result=" + _getHelloWorld__return);


        }

        System.exit(0);
    }

}

=-==================================================================

output will be as below
=-==================================================================

Invoking getHelloWorld...
getHelloWorld.result=Hello World JAX-WS John

=-==================================================================

No comments:

Post a Comment