JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Client Code Examples by Code First Approach




How to generate the stub client for jax-ws web services by wsimport java utility?
It's so simple, we need WSDL file or services publish then we can use it as end point URL in wsimport tool.

For writing the wsdl client we need to generate all the JAXWS artifacts from the wsdl. We already generated wsdl file in previous tutorial, link mentioned on top with note section.

In this session, we will learn, how to write the java web services with help of wsimport utility in jdk1.6+ and JAX-WS. 

What we need before start the code, I'm using as mention below environments, software and already installed in my system:
1. JDK1.6 or +
2. Eclipse
3. Windows OS
4. Basic knowledge of java


Let's start:


Verify the JDK installation

Open the command prompt and verify the java installation
Type the 
C:\>javac -version
javac 1.7.0_25

C:\>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)

Now java installation got verified, as installed JDK1.7.0_25


Verify the Eclipse installation


Go the the installation directory of eclipse and click on the eclipse.exe
e.g C:\eclipse\eclipse-java-mars-1-win32\eclipse\eclipse.exe

JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach


Eclipse installation got verified.


Open the Eclipse


Create the new java project
File
New
Java Project
Type the name of project: 
JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client

Run the earlier service by Click me

Verify the earlier service running or not 

Open the browser and enter the URL:
http://localhost:8088/AToZExamplesService?wsdl

You will get the WSDL details.

Uses of wsimport utility:

Open the command prompt and change the directory as current directory of client project.
e.g:

C:\workspace>cd "C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client"

C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client>

Type the wsimport command


C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client>wsimport

Missing WSDL_URI





Usage: wsimport [options] <WSDL_URI>



where [options] include:

  -b <path>                 specify jaxws/jaxb binding files or additional schemas

                            (Each <path> must have its own -b)

  -B<jaxbOption>            Pass this option to JAXB schema compiler

  -catalog <file>           specify catalog file to resolve external entity references

                            supports TR9401, XCatalog, and OASIS XML Catalog format.

  -d <directory>            specify where to place generated output files

  -extension                allow vendor extensions - functionality not specified

                            by the specification.  Use of extensions may

                            result in applications that are not portable or

                            may not interoperate with other implementations

  -help                     display help
  -httpproxy:<host>:<port>  specify a HTTP proxy server (port defaults to 8080)
  -keep                     keep generated files
  -p <pkg>                  specifies the target package
  -quiet                    suppress wsimport output
  -s <directory>            specify where to place generated source files
  -target <version>         generate code as per the given JAXWS spec version
                            Defaults to 2.2, Accepted values are 2.0, 2.1 and 2.2
                            e.g. 2.0 will generate compliant code for JAXWS 2.0 spec
  -verbose                  output messages about what the compiler is doing
  -version                  print version information
  -wsdllocation <location>  @WebServiceClient.wsdlLocation value
  -clientjar <jarfile>      Creates the jar file of the generated artifacts along with the
                            WSDL metadata required for invoking the web service.

Extensions:
  -XadditionalHeaders              map headers not bound to request or response message to
                                   Java method parameters
  -Xauthfile                       file to carry authorization information in the format
                                   http://username:password@example.org/stock?wsdl
  -Xdebug                          print debug information
  -Xno-addressing-databinding      enable binding of W3C EndpointReferenceType to Java
  -Xnocompile                      do not compile generated Java files
  -XdisableSSLHostnameVerification disable the SSL Hostname verification while fetching
                                   wsdls

Examples:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl


C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client>


We got the description of wsimport command as mention bellow which 
is self explanatory.

Now generate the client artificts with help of wsimport utility of jdk1.6 or higher

JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client



C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client>wsimport -keep -s src -verbos
e -d class http://localhost:8088/AToZExamplesService?wsdl
parsing WSDL...



Generating code...

com\atozexamples\atozexamplesservice\AToZExamplesService.java
com\atozexamples\atozexamplesservice\AToZExamplesServiceService.java
com\atozexamples\atozexamplesservice\ObjectFactory.java
com\atozexamples\atozexamplesservice\PrintGreeting.java
com\atozexamples\atozexamplesservice\PrintGreetingResponse.java
com\atozexamples\atozexamplesservice\package-info.java

Compiling code...

javac -d C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client\class -classpath C:\
java1.7\jdk1.7.0_25/lib/tools.jar;C:\java1.7\jdk1.7.0_25/classes -Xbootclasspath/p:C:\java1.7\jdk1.7.0_25\jre\lib\rt.jar;C:\java1.7\jdk1.7.0_25\jre\li
b\rt.jar C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client\src\com\atozexamples
\atozexamplesservice\AToZExamplesService.java C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First
 Approach-Client\src\com\atozexamples\atozexamplesservice\AToZExamplesServiceService.java C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 +
wsimport Utility Code Examples by Code First Approach-Client\src\com\atozexamples\atozexamplesservice\ObjectFactory.java C:\workspace\JAX-WS Java API
XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client\src\com\atozexamples\atozexamplesservice\PrintGreeting.jav
a C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client\src\com\atozexamples\atozex
amplesservice\PrintGreetingResponse.java C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Appr
oach-Client\src\com\atozexamples\atozexamplesservice\package-info.java

C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsimport Utility Code Examples by Code First Approach-Client>



Client code generated now.

AToZExamplesService.java

package com.atozexamples.atozexamplesservice;

import javax.jws.WebMethod;
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 the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 *
 */
@WebService(name = "AToZExamplesService", targetNamespace = "http://www.atozexamples.com/AToZExamplesService")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface AToZExamplesService {


    /**
     *
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "printGreeting", targetNamespace = "http://www.atozexamples.com/AToZExamplesService", className = "com.atozexamples.atozexamplesservice.PrintGreeting")
    @ResponseWrapper(localName = "printGreetingResponse", targetNamespace = "http://www.atozexamples.com/AToZExamplesService", className = "com.atozexamples.atozexamplesservice.PrintGreetingResponse")
    @Action(input = "http://www.atozexamples.com/AToZExamplesService/AToZExamplesService/printGreetingRequest", output = "http://www.atozexamples.com/AToZExamplesService/AToZExamplesService/printGreetingResponse")
    public String printGreeting();

}


AToZExamplesServiceService.java



package com.atozexamples.atozexamplesservice;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 *
 */
@WebServiceClient(name = "AToZExamplesServiceService", targetNamespace = "http://www.atozexamples.com/AToZExamplesService", wsdlLocation = "http://localhost:8088/AToZExamplesService?wsdl")
public class AToZExamplesServiceService
    extends Service
{

    private final static URL ATOZEXAMPLESSERVICESERVICE_WSDL_LOCATION;
    private final static WebServiceException ATOZEXAMPLESSERVICESERVICE_EXCEPTION;
    private final static QName ATOZEXAMPLESSERVICESERVICE_QNAME = new QName("http://www.atozexamples.com/AToZExamplesService", "AToZExamplesServiceService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8088/AToZExamplesService?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        ATOZEXAMPLESSERVICESERVICE_WSDL_LOCATION = url;
        ATOZEXAMPLESSERVICESERVICE_EXCEPTION = e;
    }

    public AToZExamplesServiceService() {
        super(__getWsdlLocation(), ATOZEXAMPLESSERVICESERVICE_QNAME);
    }

    public AToZExamplesServiceService(WebServiceFeature... features) {
        super(__getWsdlLocation(), ATOZEXAMPLESSERVICESERVICE_QNAME, features);
    }

    public AToZExamplesServiceService(URL wsdlLocation) {
        super(wsdlLocation, ATOZEXAMPLESSERVICESERVICE_QNAME);
    }

    public AToZExamplesServiceService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, ATOZEXAMPLESSERVICESERVICE_QNAME, features);
    }

    public AToZExamplesServiceService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public AToZExamplesServiceService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     *
     * @return
     *     returns AToZExamplesService
     */
    @WebEndpoint(name = "AToZExamplesServicePort")
    public AToZExamplesService getAToZExamplesServicePort() {
        return super.getPort(new QName("http://www.atozexamples.com/AToZExamplesService", "AToZExamplesServicePort"), AToZExamplesService.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns AToZExamplesService
     */
    @WebEndpoint(name = "AToZExamplesServicePort")
    public AToZExamplesService getAToZExamplesServicePort(WebServiceFeature... features) {
        return super.getPort(new QName("http://www.atozexamples.com/AToZExamplesService", "AToZExamplesServicePort"), AToZExamplesService.class, features);
    }

    private static URL __getWsdlLocation() {
        if (ATOZEXAMPLESSERVICESERVICE_EXCEPTION!= null) {
            throw ATOZEXAMPLESSERVICESERVICE_EXCEPTION;
        }
        return ATOZEXAMPLESSERVICESERVICE_WSDL_LOCATION;
    }

}



ObjectFactory.java



package com.atozexamples.atozexamplesservice;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


/**
 * This object contains factory methods for each
 * Java content interface and Java element interface
 * generated in the com.atozexamples.atozexamplesservice package.
 * <p>An ObjectFactory allows you to programatically
 * construct new instances of the Java representation
 * for XML content. The Java representation of XML
 * content can consist of schema derived interfaces
 * and classes representing the binding of schema
 * type definitions, element declarations and model
 * groups.  Factory methods for each of these are
 * provided in this class.
 *
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _PrintGreeting_QNAME = new QName("http://www.atozexamples.com/AToZExamplesService", "printGreeting");
    private final static QName _PrintGreetingResponse_QNAME = new QName("http://www.atozexamples.com/AToZExamplesService", "printGreetingResponse");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.atozexamples.atozexamplesservice
     *
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link PrintGreetingResponse }
     *
     */
    public PrintGreetingResponse createPrintGreetingResponse() {
        return new PrintGreetingResponse();
    }

    /**
     * Create an instance of {@link PrintGreeting }
     *
     */
    public PrintGreeting createPrintGreeting() {
        return new PrintGreeting();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link PrintGreeting }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://www.atozexamples.com/AToZExamplesService", name = "printGreeting")
    public JAXBElement<PrintGreeting> createPrintGreeting(PrintGreeting value) {
        return new JAXBElement<PrintGreeting>(_PrintGreeting_QNAME, PrintGreeting.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link PrintGreetingResponse }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://www.atozexamples.com/AToZExamplesService", name = "printGreetingResponse")
    public JAXBElement<PrintGreetingResponse> createPrintGreetingResponse(PrintGreetingResponse value) {
        return new JAXBElement<PrintGreetingResponse>(_PrintGreetingResponse_QNAME, PrintGreetingResponse.class, null, value);
    }

}


package-info


@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.atozexamples.com/AToZExamplesService")
package com.atozexamples.atozexamplesservice;


PrintGreeting.java



package com.atozexamples.atozexamplesservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for printGreeting complex type.
 *
 * <p>The following schema fragment specifies the expected content contained within this class.
 *
 * <pre>
 * &lt;complexType name="printGreeting">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "printGreeting")
public class PrintGreeting {


}



PrintGreetingResponse.java



package com.atozexamples.atozexamplesservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for printGreetingResponse complex type.
 *
 * <p>The following schema fragment specifies the expected content contained within this class.
 *
 * <pre>
 * &lt;complexType name="printGreetingResponse">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "printGreetingResponse", propOrder = {
    "_return"
})
public class PrintGreetingResponse {

    @XmlElement(name = "return")
    protected String _return;

    /**
     * Gets the value of the return property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getReturn() {
        return _return;
    }

    /**
     * Sets the value of the return property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setReturn(String value) {
        this._return = value;
    }

}



Writhe the calling or AToZExamplesClient which will have the client code and main method

Right click on the src folder
New
Class
Type the name:AToZExamplesClient
Type the package name:com.atozexamples.client
Finish

AToZExamplesClient.java


package com.atozexamples.client;

import com.atozexamples.atozexamplesservice.AToZExamplesService;
import com.atozexamples.atozexamplesservice.AToZExamplesServiceService;

public class AToZExamplesClient {

       public AToZExamplesClient() {
              // TODO Auto-generated constructor stub
       }

       public static void main(String[] args) {
              //
              AToZExamplesServiceService objAToZExamplesServiceService = new AToZExamplesServiceService();
              AToZExamplesService objMathService = objAToZExamplesServiceService.getAToZExamplesServicePort();
              System.out.println(objMathService.printGreeting());

       }

}



Run the  AToZExamplesClient

Right click, run as, java application.

Out put:

Hello from AToZExamples.com!!

Happy learning and implementation!!!

No comments:

Post a Comment