Top Down vs Bottom Up in SOAP
In the context of SOAP web services, two main approaches can be used to develop and deploy services: the top-down approach and the bottom-up approach. Each method has its own set of advantages and is chosen based on specific project requirements.
Table of Contents
Top Down Approach
The top-down approach starts with the creation of a WSDL (Web Services Description Language) file that defines the web service interface. This WSDL file is used as a contract to generate the server-side code skeleton and client-side code for invoking the service.
Steps:
- 1. Create the WSDL file, which specifies the service interface.
- 2. Use a tool (like wsimport or WSDL2Java) to generate the server-side code skeleton.
- 3. Implement the business logic in the generated code.
- 4. Deploy the service.
Advantages:
- Ensures a clear contract (WSDL) from the beginning.
- Better suited for designing services with well-defined interfaces.
- Promotes a more disciplined approach to service design.
Disadvantages:
- Can be more time-consuming initially as it requires careful WSDL design.
- Requires knowledge of WSDL and XML Schema.
Bottom Up Approach
The bottom-up approach starts with the implementation of the business logic (Java code), and then the WSDL file is generated from this implementation. This method is typically quicker and easier for developers who are more comfortable starting with code.
Steps:
- 1. Implement the business logic in Java.
- 2. Use a tool (like wsgen) to generate the WSDL file from the Java code.
- 3. Deploy the service.
Advantages:
- Faster initial development, especially for developers familiar with Java.
- Easier to start with existing business logic.
Disadvantages:
- The WSDL may not be as well-structured or optimized as when designed first.
- May lead to less emphasis on service design and interface definition.
Explanation in Java Example
To illustrate these approaches, we will use a simple SOAP web service example in Java.
xml
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example.com/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="HelloWorldService"
targetNamespace="http://example.com/">
<types>
<xsd:schema>
<xsd:element name="sayHello" type="xsd:string"/>
<xsd:element name="sayHelloResponse" type="xsd:string"/>
</xsd:schema>
</types>
<message name="sayHelloRequest">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="HelloWorld">
<operation name="sayHello">
<input message="tns:sayHelloRequest"/>
<output message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloWorldSoapBinding" type="tns:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<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:HelloWorldSoapBinding">
<soap:address location="http://localhost:8080/helloWorld"/>
</port>
</service>
</definitions>
Use a tool like wsimport to generate the server-side code:
sh
wsimport -keep -p com.example -d src -s src HelloWorldService.wsdl
java
package com.example;
import javax.jws.WebService;
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
java
import javax.xml.ws.Endpoint;
public class HelloWorldPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/helloWorld", new HelloWorldImpl());
System.out.println("Service is published!");
}
}
Bottom Up Approach Example
java
import javax.jws.WebService;
@WebService
public class HelloWorld {
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
Use a tool like wsgen to generate the WSDL file:
sh
wsgen -keep -cp . -d wsdl -r wsdl -s src -wsdl com.example.HelloWorld
java
import javax.xml.ws.Endpoint;
public class HelloWorldPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/helloWorld", new HelloWorld());
System.out.println("Service is published!");
}
}