SOAP(Simple Object Access Protocol) is one of the popular way of inter application communication, application can be on different system or same system using network, can on different languages. SOAP can work with many protocol such http, SMTP etc. SOAP has two flavour RPC Style and Document based. SOAP has strict contract defined in form of WSDL. You can start creating Java class and later define WSDL based on class(bottom up approach) or define WSDL and generate Java class(top down approach). Defining WSDL can be tedious sometime for complex services so top down can be tedious compared bottom up approach.

What is WSDL.

WSDL is contract definition written in form of single XML. It contains message type of request and response, endpoints etc. Here is the detailed list of WSDL component.

<definitions name = "EmployeeService"
   targetNamespace = "http://www.examples.com/wsdl/EmployeeService.wsdl"
   xmlns = "http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns = "http://www.examples.com/wsdl/EmployeeService.wsdl"
   xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
 
   <types>
      to define complex types if there are any........
   </types>
   <message name = "GetEmployeeRequest">
      <part name = "id" type = "xsd:string"/>
   </message>
	
   <message name = "GetEmployeeResponse">
      <part name = "name" type = "xsd:string"/>
   </message>

   <portType name = "Employee_PortType">
      <operation name = "getEmployee">
         <input message = "tns:GetEmployeeRequest"/>
         <output message = "tns:GetEmployeeResponse"/>
      </operation>
   </portType>

   <binding name = "Employee_Binding" type = "tns:Employee_PortType">
      <soap:binding style = "rpc"
         transport = "http://schemas.xmlsoap.org/soap/http"/>
      <operation name = "getEmployee">
         <soap:operation soapAction = "getEmployee"/>
         <input>
            <soap:body
               encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/"
               namespace = "urn:examples:employeeservice"
               use = "encoded"/>
         </input>
         <output>
            <soap:body
               encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/"
               namespace = "urn:examples:employeeservice"
               use = "encoded"/>
         </output>
      </operation>
   </binding>

   <service name = "EmployeeService">
      <documentation>WSDL File for EmployeeService</documentation>
      <port binding = "tns:Employee_Binding" name = "Employee_Port">
         <soap:address
            location = "http://www.examples.com/GetEmployee/" />
      </port>
   </service>
</definitions>

RPC vs Document Based.

These two are type of message formate. Document based is full fledged complex message which can have multiple for itself while RPC is straight forward input output parameter for a method. You can say Document follows Object based approach and easy to map to and from a Java object while, RPC based is just a simple input output parameter based on that specific method.

Top down approach.

Java command line tool

Java provides a small tool to generate Java classes based on defined WSDL. It can create a server side classes as well as client side also. wsimport -s . -p com.xyz.jaxws.server.topdown employeeservice.wsdl
It will creates three classes EmployeeService.java, EmployeeService_Service.java and ObjectFactory.java

@WebService(
  name = "EmployeeService", 
  targetNamespace = "******")
@SOAPBinding(style=Style.RPC, use=Use.ENCODED)
@XmlSeeAlso({
    ObjectFactory.class
})
public interface EmployeeService {
    @WebMethod(
      action = "*******"
      + "EmployeeService/getEmployee")
    @WebResult(
      name = "GetEmployeeResponse", 
      targetNamespace = "********", 
      partName = "name")
    public String getEmployee(int id);
}

@WebService(endpointInterface="com.xyz.EmployeeService")
public interface EmployeeServiceImpl implements  EmployeeService{
  @Override
    public String getEmployee(int id){
      return employeeRepo.get(id);
    }
}

Eclipse Web service generator .

Even eclipse provide support to create the classes automatically based on WSDL. eclipse

Bottom up Approach

In bottom up we create a class with some methods and then create service out of it.

Java approach.

Use @WebService, @WebMethod and @WebResult annotation to annotate the class and methods. It will somewhat same as generated class file from java or eclipse tool. Once you publish the service wsdl will be available on the URL.
http://localhost/EmployeeService?wsdl

Eclipse approach.

Create a simple bean with methods which needs to be expose. Then in eclipse select bottom up approach and select the bean. It will create helper classes and WSDL automatically. eclipse

Creating client.

Eclipse tool - In both the top down and bottom up approach you have option to create client as well. It creats separate project and creates client classes. Java command line tool - Use following class to create client classes automatically. wsimport -keep -p com.xyz.jaxws.client http://localhost/employeeService?wsdl

public class EmployeeServiceClient {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost/employeeService?wsdl");
 
        EmployeeService employeeService 
          = new EmployeeService(url);
        EmployeeService employeeServiceProxy 
          = employeeService.getEmployeeServiceImplPort();
 
        String name 
          = employeeServiceProxy.getEmployees(1);
    }
}

Publishing WebService

In the end you would need to publish the web service and make it available.

Endpoint.publish(
          "http://localhost:8080/employeeService", 
           new EmployeeServiceImpl());