Create a sample project using SpringInitializr choose web services, jpa, h2(in memory db) dependencies and generate the project. Unzip it and import it to preferred IDE.

Spring 'webservices' provides contract first approach in developing web services, it means you will define the structure of request and response.

  1. create a folder and put in the request XML and response XML ( request may contain like get the course with id =1 ,  may have like id, name and details).      <CourseRequest xmlns ="http://examples.com/courses" xsi:schemaLocation = "{namespace} { location(sample)}" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"><id>1</id></CourseRequest> and similar structure for response with id , name and details.
  2. XSD (XML Schema Definition) should be defined. With XSD we get setup restrictions on the fields of request and response XML's.
    • create XML Schema file and save it in the folder created(sample.xsd).
    • put in id can only be integer <element name = "CourseRequest"><complexType><sequence><element name = "id" type = "integer"></sequence></complexType></element>. More info on XSD can be found here..
  3. We should be able to convert the XSD (make it available to application) to java objects for this use jaxb2maven plugin.
  4. <plugin>
    
                <groupId>org.codehaus.mojo</groupId>
    
                <artifactId>jaxb2-maven-plugin</artifactId>
    
                <executions>
    
                    <execution>
    
                        <goals>
    
                            <goal>xjc</goal>
    
                        </goals>
    
                    </execution>
    
                </executions>
    
                <configuration>
    
                    <!-- The schema directory or xsd files. -->
    
                    <schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
    
                    <!-- The package in which the source files will be generated. -->
    
                    <packageName>com.service.bean.demo</packageName>
    
                     <clearOutputDir>false</clearOutputDir>
    
                    <!-- The working directory to create the generated java source files. -->
    
                    <outputDirectory>${basedir}/src/main/java</outputDirectory>
    
                </configuration>
    
            </plugin>
  5. Create Endpoint - will typically have a method which takes in input request and responds back with a response. Use @Endpoint on class, @PayLoadRoot(namespace= "put xmlns here..", localPart = "name of the request") on method, now to map xml to java object use @RequestPayLoad in method args, @ResponsePayload on method to convert the response java object to XML.
  6. Next we need to define URL - that is configuring the endpoint. Generally used name is WebConfig Class, use @Enablews, @Configuration on class. Configure Message Dispatcher Servlet which is to identify endpoints.
  7. Now we need to generate WSDL. Web services Description Language is like an interface ie., the structure of the XML messages that the service can accept/return. ( we will need dependency like wsdl4j).
  8. Now we can use SOAP clients like SOAPUI or Wizdler and test your services.