Thursday, 1 November 2012

Contract-first Spring web services example using JAXB-XJC tool


Developing a contract-first Spring web services JAXB-XJC tool

In the earlier link developing-contract-first-spring-web.html, I have provided the details to create a sample application using contract-first spring web services.  Now we will proceed further to create another application using contract-first spring web services.

Approximately all the steps are mentioned in the above are similar.

1) Create a web project in Eclipse and name it as ContractFirstStudentService.


2) Writing the Student.xsd file for Student as below:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://localhost:7070/gaurav/springws/ContractFirstStudentService"
    xmlns:tns="http://localhost:7070/gaurav/springws/ContractFirstStudentService" elementFormDefault="qualified">

    <element name="studentRequest">
        <complexType>
            <sequence>
                <element name="id" type="string"></element>
            </sequence>
        </complexType>
    </element>
    <element name="studentResponse">
        <complexType>
            <sequence>
            <element name="studentdetails" type="tns:studentdetails"></element>
            </sequence>
        </complexType>
    </element>
    <complexType name="studentdetails">
        <sequence>
            <element name="name" type="string"></element>
            <element name="total" type="string"></element>
            <element name="address" type="tns:address"></element>
           
        </sequence>
    </complexType>
    <complexType name="address">
        <sequence>
           
            <element name="flatNo" type="string"></element>
            <element name="street" type="string"></element>
            <element name="city" type="string"></element>
            <element name="state" type="string"></element>
            <element name="pin" type="string"></element>
        </sequence>
    </complexType>
</schema>


3) Get the plugin of JAXB 2.0 for Eclipse Helios or the eclipse version which you are using. Keep the required jar in the Eclipse plugin folder.

4) Right Click on the Student.xsd file, go to JAXB 2.0 option and click on Run XJC option, in the prompt window give package name as gaurav.springws.studentservice.service and in the output directory point it to src folder of ContractFirstStudentService eclipse project.

5) It will generate the classes corresponding as mentioned in the xsd file, which is shown below:



6) Create a service interface class named as StudentService.java in gaurav.springws.student
service.service package. Use the below code:-

StudentService.java
 
package gaurav.springws.studentservice.service;

public interface StudentService {

    Studentdetails getStudentDetails(Long empId);

}

7) Create a service interface implementation class and name it as StudentServiceImpl.java in the package gaurav.springws.studentservice.service. Use the below code:-

StudentServiceImpl.java

package gaurav.springws.studentservice.service;

import org.apache.log4j.Logger;

public class StudentServiceImpl implements StudentService {

    private static final Logger logger = Logger
            .getLogger(StudentServiceImpl.class);

    public StudentServiceImpl() {
    }

    public Studentdetails getStudentDetails(Long empId) {
        logger.info("Id has been provided. EmpID : " + empId);
        String name = null;

        Studentdetails sd = new Studentdetails();

        if (empId == 1) {
            name = "Gaurav";

        } else {
            name = "Shivam";

        }
        sd.setName(name);
        sd.setTotal("895");
        Address address = new Address();
        address.setFlatNo("630");
        address.setStreet("Kondapur");
        address.setCity("Hyderabad");
        address.setState("Andhra Pradesh");
        address.setPin("500048");
        sd.setAddress(address);
        
        Address address1 = sd.getAddress();
        return sd;
    }

}

8) Create a package and name it as gaurav.springws.studentservice.service.endpoint.
  •   Create a class name as StudentServicePayloadRootAnnotationEndPoint.java
  •   Use the below code in the jave file :-
StudentServicePayloadRootAnnotationEndPoint.java
package gaurav.springws.studentservice.service.endpoint;

import gaurav.springws.studentservice.service.ObjectFactory;
import gaurav.springws.studentservice.service.StudentRequest;
import gaurav.springws.studentservice.service.StudentResponse;
import gaurav.springws.studentservice.service.StudentService;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

@Endpoint
public class StudentServicePayloadRootAnnotationEndPoint {

    private final StudentService studService;
    private final ObjectFactory JAXB_OBJECT_FACTORY = new ObjectFactory();

    public StudentServicePayloadRootAnnotationEndPoint(
            StudentService studentService) {
        this.studService = studentService;
    }

    @PayloadRoot(localPart = "studentRequest", namespace = "http://localhost:7070/gaurav/springws/ContractFirstStudentService")
    public JAXBElement<StudentResponse> getStudentDetails(
            StudentRequest studRequest) {
        StudentResponse response = JAXB_OBJECT_FACTORY.createStudentResponse();
        System.out.println("studRequest.getId()-->" + studRequest.getId());

        response.setStudentdetails(studService.getStudentDetails(Long
                .parseLong(studRequest.getId())));

        return new JAXBElement<StudentResponse>(
                new QName(
                        "http://localhost:7070/gaurav/springws/ContractFirstStudentService",
                        "studentResponse"), StudentResponse.class, response);
    }

}


9) modify the web.xml file as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>ContractFirstStudentService</display-name>
    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>
            org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

10) Create the spring-ws-servlet.xml file, as spring-ws is the name of the servlet mentioned in the web.xml. So it's rule to create the bean file as the name of the servlet which should be prefixed with -servlet. Like spring-ws-servlet.xml. Use the below code in the spring-ws-servlet.xml file.

 <?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context                      
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- PayloadRootAnnotationMethodEndpointMapping is the Mapping that detects
        and handles the @PayloadRoot Annotation -->
    <bean
        class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
        <property name="interceptors">
            <list>
                <bean
                    class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
            </list>
        </property>
    </bean>

    <bean id="studServiceEndpoint"
        class="gaurav.springws.studentservice.service.endpoint.StudentServicePayloadRootAnnotationEndPoint">
        <constructor-arg>
            <bean class="gaurav.springws.studentservice.service.StudentServiceImpl" />
        </constructor-arg>
    </bean>

    <bean id="StudService"
        class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
        <property name="schema" ref="studServiceSchema" />
        <property name="portTypeName" value="StudService" />
        <property name="locationUri"
            value="http://localhost:7070/ContractFirstStudentService/services" />
        <property name="targetNamespace"
            value="http://localhost:7070/gaurav/springws/ContractFirstStudentService" />
    </bean>

    <bean id="studServiceSchema" class="org.springframework.xml.xsd.SimpleXsdSchema">
        <property name="xsd" value="/WEB-INF/Student.xsd" />
    </bean>

    <bean
        class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
        <constructor-arg ref="marshaller" />
    </bean>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="gaurav.springws.studentservice.service" />
    </bean>

</beans>  

11) The required jars for this project is shown below:-



12) The final architecture of the project is as below:-




13) Select the application, Right Click on it, go to Run As option and select Run On Server. Configure the Tomcat server 6.0 or 7.0, on the port 7070, as I am running my tomcat 6.0 on the port 7070.


14) After running the server, hit the below URL:
http://localhost:7070/ContractFirstStudentService/services/StudService.wsdl

Note :--> http://localhost:7070/ContractFirstStudentService/services - This is the Location URI mentioned in spring-contractfirst-webservice-servlet.xml file. and StudService is the portTypeName.

15) After hitting the above URL the WSDL which we will get is shown below:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://localhost:7070/gaurav/springws/ContractFirstStudentService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:7070/gaurav/springws/ContractFirstStudentService" targetNamespace="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
- <wsdl:types>
- <schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
- <element name="studentRequest">
- <complexType>
- <sequence>
  <element name="id" type="string" />
  </sequence>
  </complexType>
  </element>
- <element name="studentResponse">
- <complexType>
- <sequence>
  <element name="studentdetails" type="tns:studentdetails" />
  </sequence>
  </complexType>
  </element>
- <complexType name="studentdetails">
- <sequence>
  <element name="name" type="string" />
  <element name="total" type="string" />
  <element name="address" type="tns:address" />
  </sequence>
  </complexType>
- <complexType name="address">
- <sequence>
  <element name="flatNo" type="string" />
  <element name="street" type="string" />
  <element name="city" type="string" />
  <element name="state" type="string" />
  <element name="pin" type="string" />
  </sequence>
  </complexType>
  </schema>
  </wsdl:types>
- <wsdl:message name="studentRequest">
  <wsdl:part element="tns:studentRequest" name="studentRequest" />
  </wsdl:message>
- <wsdl:message name="studentResponse">
  <wsdl:part element="tns:studentResponse" name="studentResponse" />
  </wsdl:message>
- <wsdl:portType name="StudService">
- <wsdl:operation name="student">
  <wsdl:input message="tns:studentRequest" name="studentRequest" />
  <wsdl:output message="tns:studentResponse" name="studentResponse" />
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="StudServiceSoap11" type="tns:StudService">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="student">
  <soap:operation soapAction="" />
- <wsdl:input name="studentRequest">
  <soap:body use="literal" />
  </wsdl:input>
- <wsdl:output name="studentResponse">
  <soap:body use="literal" />
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="StudServiceService">
- <wsdl:port binding="tns:StudServiceSoap11" name="StudServiceSoap11">
  <soap:address location="http://localhost:7070/ContractFirstStudentService/services" />
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions> 

16) After getting the WSDL, Use this http://localhost:7070/ContractFirstStudentService/services/StudService.wsdl  URL in SoapUI for Test purpose - I am using SoapUI 3.0.1. 
  • Go to File, select New SoapUI Project, put the above URL in Initial WSDL/WADL option.
  • After placing this URL, it will automatically create a request like below:
 Request:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
   <soapenv:Header/>
   <soapenv:Body>
      <con:studentRequest>
         <con:id>?</con:id>
      </con:studentRequest>
   </soapenv:Body>
</soapenv:Envelope>

Note:- Pass the student id in place of question mark.( I passed 1 in place of Question Mark).

Response:-   

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:studentResponse xmlns:ns2="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
         <ns2:studentdetails>
            <ns2:name>Gaurav</ns2:name>
            <ns2:total>895</ns2:total>
            <ns2:address>
               <ns2:flatNo>630</ns2:flatNo>
               <ns2:street>Kondapur</ns2:street>
               <ns2:city>Hyderabad</ns2:city>
               <ns2:state>Andhra Pradesh</ns2:state>
               <ns2:pin>500048</ns2:pin>
            </ns2:address>
         </ns2:studentdetails>
      </ns2:studentResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope> 

Note:- Pass the other student id like 2,3 or any number.

Response:- 

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:studentResponse xmlns:ns2="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
         <ns2:studentdetails>
            <ns2:name>
Shivam</ns2:name>
            <ns2:total>895</ns2:total>
            <ns2:address>
               <ns2:flatNo>630</ns2:flatNo>
               <ns2:street>Kondapur</ns2:street>
               <ns2:city>Hyderabad</ns2:city>
               <ns2:state>Andhra Pradesh</ns2:state>
               <ns2:pin>500048</ns2:pin>
            </ns2:address>
         </ns2:studentdetails>
      </ns2:studentResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 

Note :- This Sample application is developed with the help of JAXB 2.0 Run XJC tool by using Spring web services.
 

No comments:

Post a Comment