Writing Web Services  Locate

Overview  Locate

This chapter shows the first steps of creating and implementing a Web service. Note that the source code contains no signs it is a Web service. This is one of the things that makes WSO2 SOA Enablement Server such a powerful tool. This chapter Starting from Java and Starting From a WSDL Description.

Starting from Java  Locate

For simplicity's sake we implement the HelloServiceImpl class with one method. The logic of this Web service is given in Example 1.

Example 1. HelloService Implementation

// Copyright WSO2 Inc. All rights reserved.
// Use is subject to license terms.
package example.basics.webservices;

public class HelloServiceImpl {
  public String hello(String message) {
    return "Hello, " + message + "!";
  }
}

After compiling HelloServiceImpl.java, you can publish this service as described in the Publishing chapter.

Starting From a WSDL Description  Locate

In some cases, the WSDL file is created beforehand as a part of interface design. WSO2 SOA Enablement Server can generate the service interface and necessary data structures from the WSDL. Example 2 shows a WSDL file for the Web service implementation in Example 1. You can also use WSO2 SOA Enablement Server to generate a skeleton of the implementation, as in the Java2WSDL command shown later in this chapter. For details, see the WSDL2Java chapter in Tools.

Example 2. HelloService WSDL

<?xml version='1.0' encoding='utf-8' ?>
<wsdl:definitions name="example.basics.webservices.HelloService"
    targetNamespace="http://wso2.com/wsdl/example/basics/webservices/"
    xmlns:ns0="http://wso2.com/xsd/SchemaTypes/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://wso2.com/wsdl/example/basics/webservices/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <wsdl:types>
        <xsd:schema elementFormDefault="qualified"
            targetNamespace="http://wso2.com/xsd/SchemaTypes/"
            xmlns:tns="http://wso2.com/xsd/SchemaTypes/" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsd:element name="message" nillable="true" type="xsd:string"/>
            <xsd:element name="string_Response" nillable="true" 
                type="xsd:string"/>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="HelloService_hello_1_Request">
        <wsdl:part element="ns0:message" name="message"/>
    </wsdl:message>
    <wsdl:message name="HelloService_hello_Response">
        <wsdl:part element="ns0:string_Response" name="response"/>
    </wsdl:message>
    <wsdl:portType name="HelloService">
        <wsdl:operation name="hello" parameterOrder="message">
            <wsdl:input message="tns:HelloService_hello_1_Request"/>
            <wsdl:output message="tns:HelloService_hello_Response"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="HelloService" type="tns:HelloService">
        <soap:binding style="document" 
            transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="hello">
            <soap:operation
                soapAction="http://wso2.com/wsdl/example/basics/webservices/
HelloServiceImpl#hello#KExqYXZhL2xhbmcvU3RyaW5nOylMamF2YS9sYW5nL1N0cmluZzs=" 
                style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="HelloService">
        <wsdl:port binding="tns:HelloService" name="HelloService">
            <soap:address location="urn:unknown-location-uri"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

The following command shows how to create a skeleton of the implementation from the WSDL:

WSDL2Java HelloService.wsdl --interface-package example.basics.webservices --output-language java-impl

Now you can implement this skeleton as it is implemented in Example 1. After compiling HelloServiceImpl.java, you can publish this service to the web as described in Publishing.

[Note]Note

You may notice that WSDL2Java also generates a HelloServiceImpl.xmap. You do not need to concern yourself with this file at this time. The .xmap file is used in certain cases when the Web service is invoked on the server. For more information, see Mapping Files in Java Client, Web Service Invocation.