Create Web Service using Java + Eclipse

Web Service is a term used for service provided over network and always accessible at the mentioned web address for its consumers. The backbone of web service are
a) WSDL (Web Service Definition Language): WSDL is an XML-based language for describing Web services.
b) SOAP (Simple Object Access Protocol): SOAP is an XML based protocol for accessing Web Services.
c) UDDI (Universal Description, Discovery and Integration): UDDI is a directory service where companies can search for Web services.

Step-by-Step guide for creating a web service using Eclipse IDE are as listed below:
1. Create a new Dynamic Web Project
CreateWebServiceEclipse_001

2. Define Project Name and map the Target Runtime
CreateWebServiceEclipse_002

3. Click Finish.
4. Create Java classes – we will use below Java classes for this example


package com.dirtyyourhands;

import java.util.ArrayList;
import java.util.List;

public class School {
  List<Student> students;

  public School() {
    students = new ArrayList<Student>();
  }

  public void addStudent(Student student) {
    students.add(student);
  }

  public boolean findStudent(Student student) {
    if (students.contains(student))
      return true;

    return false;
  }
}


package com.dirtyyourhands;

public class Student {
  String firstName;
  String lastName;

  int rollNumber;

  public Student() {
  }

  /**
   * @param firstName
   * @param lastName
   * @param rollNumber
   */
  public Student(String firstName, String lastName, int rollNumber) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.rollNumber = rollNumber;
  }

  /**
   * @return the firstName
   */
  public String getFirstName() {
    return firstName;
  }

  /**
   * @param firstName
   *            the firstName to set
   */
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  /**
   * @return the lastName
   */
  public String getLastName() {
    return lastName;
  }

  /**
   * @param lastName
   *            the lastName to set
   */
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  /**
   * @return the rollNumber
   */
  public int getRollNumber() {
    return rollNumber;
  }

  /**
   * @param rollNumber
   *            the rollNumber to set
   */
  public void setRollNumber(int rollNumber) {
    this.rollNumber = rollNumber;
  }

}

5. Right click the School class, and select Web Services -> Create Web Service
6. In Web Service Window, make sure School class is mapped as Implementation class, In Service and Configuration, slide to “Test Service” and “Test Client” and check the checkbox for Publish the Web Service, as shown in below screen
CreateWebServiceEclipse_003

7. Click Next. On this screen, you define which methods you want to expose, name the wsdl file and, select style (for this example we take it as ‘document/literal’)
CreateWebServiceEclipse_004

8. Click Next, it will generate the client code for us.
CreateWebServiceEclipse_005
9. Click Start server. We have the Webservice deployed on Tomcat server we defined now.
10. Let’s test the web service, Click next and you will see screen like below
CreateWebServiceEclipse_006

11. Click Launch and you can see screen like below
CreateWebServiceEclipse_007

12. Click next on Test Web Service, you will reach screen to create Web Service Proxy
CreateWebServiceEclipse_008

13. Click Next, you will find details of the proxy defined
CreateWebServiceEclipse_009

14. Click Next
CreateWebServiceEclipse_010

15. The generated Web Service Client will look like below
CreateWebServiceEclipse_011

16. Your wsdl file will looks like as below


<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://dirtyyourhands.com"
  xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://dirtyyourhands.com"
  xmlns:intf="http://dirtyyourhands.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <!--WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT) -->
  <wsdl:types>
    <schema elementFormDefault="qualified" targetNamespace="http://dirtyyourhands.com"
      xmlns="http://www.w3.org/2001/XMLSchema">
      <element name="addStudent">
        <complexType>
          <sequence>
            <element name="student" type="impl:Student" />
          </sequence>
        </complexType>
      </element>
      <complexType name="Student">
        <sequence>
          <element name="firstName" nillable="true" type="xsd:string" />
          <element name="lastName" nillable="true" type="xsd:string" />
          <element name="rollNumber" type="xsd:int" />
        </sequence>
      </complexType>
      <element name="addStudentResponse">
        <complexType />
      </element>
      <element name="findStudent">
        <complexType>
          <sequence>
            <element name="student" type="impl:Student" />
          </sequence>
        </complexType>
      </element>
      <element name="findStudentResponse">
        <complexType>
          <sequence>
            <element name="findStudentReturn" type="xsd:boolean" />
          </sequence>
        </complexType>
      </element>
    </schema>
  </wsdl:types>
  <wsdl:message name="addStudentResponse">
    <wsdl:part element="impl:addStudentResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="findStudentResponse">
    <wsdl:part element="impl:findStudentResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="addStudentRequest">
    <wsdl:part element="impl:addStudent" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="findStudentRequest">
    <wsdl:part element="impl:findStudent" name="parameters"/>
  </wsdl:message>
  <wsdl:portType name="School">
    <wsdl:operation name="addStudent">
      <wsdl:input message="impl:addStudentRequest" name="addStudentRequest"/>
      <wsdl:output message="impl:addStudentResponse" name="addStudentResponse"/>
    </wsdl:operation>
    <wsdl:operation name="findStudent">
      <wsdl:input message="impl:findStudentRequest" name="findStudentRequest"/>
      <wsdl:output message="impl:findStudentResponse" name="findStudentResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="SchoolSoapBinding" type="impl:School">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="addStudent">
      <wsdlsoap:operation soapAction="" />
      <wsdl:input name="addStudentRequest">
        <wsdlsoap:body use="literal" />
      </wsdl:input>
      <wsdl:output name="addStudentResponse">
        <wsdlsoap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="findStudent">
      <wsdlsoap:operation soapAction="" />
      <wsdl:input name="findStudentRequest">
        <wsdlsoap:body use="literal" />
      </wsdl:input>
      <wsdl:output name="findStudentResponse">
        <wsdlsoap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SchoolService">
    <wsdl:port binding="impl:SchoolSoapBinding" name="School">
      <wsdlsoap:address location="http://localhost:8089/StudentWebService/services/School" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>