×
☰ See All Chapters

wsgen example

In this wsgen example tutorial you will learn to use wsgen tool to generate JAX-WS portable artifacts. wsgen  tool reads a web service endpoint class and generates all the required artifacts for web service deployment, and invocation. JAXWS 2.1.1 RI also provides a wsgen ant task, refer Wsgen ant task for details. For more details on wsgen command please refer our wsgen previous chapter.

Two common use cases for wsgen tool :

  1. Generates JAX-WS portable artifacts (Java files) for web service development. 

  2. Generates WSDL and xsd files, for testing or web service client development. 

Generates JAX-WS portable artifacts (Java files)

HelloWorld.java

package com.java4coding;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

 

@WebService

public class HelloWorld {

        @WebMethod

        public String getHelloWorldAsString(String name) {

                return "Hello " + name;

        }

}

To generate all the JAX-WS portable artifacts for above web service implementation class (HelloWorld.java), follow following steps.

  1. Create directory structure as suitable for package com.java4coding 

  2. Create HelloWorld.java file inside java4coding directory. 

  3. Open command window in java4coding directory and compile HelloWorld.java file and generate HelloWorld.class file. (execute the command: javac HelloWorld.java) 

wsgen-example-0
 
  1. Open command window for parent directory of com directory and execute the following command 

wsgen -verbose -keep -cp . com.java4coding.HelloWorld

wsgen-example-1
 

In this case, it generated four files

  1. com\java4coding\jaxws\GetHelloWorldAsString.class 

  2. com\ java4coding \jaxws\GetHelloWorldAsString.java 

  3. com\ java4coding \jaxws\GetHelloWorldAsStringResponse.class 

  4. com\ java4coding \jaxws\GetHelloWorldAsStringResponse.java 

wsgen-example-2
 

GetHelloWorldAsString.java

package com.java4coding.jaxws;

 

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;

 

@XmlRootElement(name = "getHelloWorldAsString", namespace = "https://java4coding.com/")

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "getHelloWorldAsString", namespace = "https://java4coding.com/")

public class GetHelloWorldAsString {

 

    @XmlElement(name = "arg0", namespace = "")

    private String arg0;

 

    /**

     *

     * @return

     *     returns String

     */

    public String getArg0() {

        return this.arg0;

    }

 

    /**

     *

     * @param arg0

     *     the value for the arg0 property

     */

    public void setArg0(String arg0) {

        this.arg0 = arg0;

    }

}

GetHelloWorldAsStringResponse.java

package com.java4coding.jaxws;

 

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;

 

@XmlRootElement(name = "getHelloWorldAsStringResponse", namespace = "https://java4coding.com/")

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "getHelloWorldAsStringResponse", namespace = "https://java4coding.com/")

public class GetHelloWorldAsStringResponse {

 

    @XmlElement(name = "return", namespace = "")

    private String _return;

 

    /**

     *

     * @return

     *     returns String

     */

    public String getReturn() {

        return this._return;

    }

 

    /**

     *

     * @param _return

     *     the value for the _return property

     */

    public void setReturn(String _return) {

        this._return = _return;

    }

 

}

Generate WSDL and xsd

To generate WSDL and xsd files for above web service implementation class ( HelloWorld.java), add an extra wsdl in the wsgen command:

wsgen -verbose -keep -cp . com.java4coding.HelloWorld –wsdl

wsgen-example-3
 
wsgen-example-4
 

In this case it generates six files

  1. com\java4coding\jaxws\GetHelloWorldAsString.class 

  2. com\ java4coding \jaxws\GetHelloWorldAsString.java 

  3. com\ java4coding \jaxws\GetHelloWorldAsStringResponse.class 

  4. com\ java4coding \jaxws\GetHelloWorldAsStringResponse.java 

  5. HelloWorldService.wsdl 

  6. HelloWorldService_schema1.xsd 

HelloWorldService_schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" targetNamespace="https://java4coding.com/" xmlns:tns="https://java4coding.com/" xmlns:xs="https://www.w3.org/2001/XMLSchema">

 

  <xs:element name="getHelloWorldAsString" type="tns:getHelloWorldAsString"/>

 

  <xs:element name="getHelloWorldAsStringResponse" type="tns:getHelloWorldAsStringResponse"/>

 

  <xs:complexType name="getHelloWorldAsString">

    <xs:sequence>

      <xs:element name="arg0" type="xs:string" minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

 

  <xs:complexType name="getHelloWorldAsStringResponse">

    <xs:sequence>

      <xs:element name="return" type="xs:string" minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:schema>

HelloWorldService.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<!-- Generated by JAX-WS RI (https://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->

<definitions targetNamespace="https://java4coding.com/" name="HelloWorldService" xmlns="https://schemas.xmlsoap.org/wsdl/" xmlns:wsp="https://www.w3.org/ns/ws-policy" xmlns:wsu="https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp1_2="https://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="https://java4coding.com/" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="https://www.w3.org/2007/05/addressing/metadata">

  <types>

    <xsd:schema>

      <xsd:import namespace="https://java4coding.com/" schemaLocation="HelloWorldService_schema1.xsd"/>

    </xsd:schema>

  </types>

  <message name="getHelloWorldAsString">

    <part name="parameters" element="tns:getHelloWorldAsString"/>

  </message>

  <message name="getHelloWorldAsStringResponse">

    <part name="parameters" element="tns:getHelloWorldAsStringResponse"/>

  </message>

  <portType name="HelloWorld">

    <operation name="getHelloWorldAsString">

      <input wsam:Action="https://java4coding.com/HelloWorld/getHelloWorldAsStringRequest" message="tns:getHelloWorldAsString"/>

      <output wsam:Action="https://java4coding.com/HelloWorld/getHelloWorldAsStringResponse" message="tns:getHelloWorldAsStringResponse"/>

    </operation>

  </portType>

  <binding name="HelloWorldPortBinding" type="tns:HelloWorld">

    <soap:binding transport="https://schemas.xmlsoap.org/soap/http" style="document"/>

    <operation name="getHelloWorldAsString">

      <soap:operation soapAction=""/>

      <input>

        <soap:body use="literal"/>

      </input>

      <output>

        <soap:body use="literal"/>

      </output>

    </operation>

  </binding>

  <service name="HelloWorldService">

    <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">

      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>

    </port>

  </service>

</definitions>

 


All Chapters
Author