Simple Webservice Example

Download The source code Apache Cxf.

Cxf support two approach.

  • Contract first(WSDL first or top-down)
  • Contract last (Code first or bottom-up)

Developing a web service example using Apache cxf contract first..


In Contract first approach we will have the requirement in the form of xsd.we have to understand the clear requirement from  xsd.
A xsd contains request and response.In otherwords what will be sending as  a request and what will be getting as a response that will be specified in the xsd.

It is nothing but like a java pojo class content which has getter and setter method..
A typical xsd has tag like complex type ,element type tag like....

What is the requirment and what is the resonse we will have to specify in xsd before developing the web service  in the form of contact in xsd ,that why it is called contarct first web service approach.

A simple Contract first Example.

Step-1                               Create a maven directory Struture.

Src/main/java
src/main/resources
src/main/webapp      ..                                                 






Step-2                                          .myschema .xsd file.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.cxftest.org" elementFormDefault="qualified">


<!-- Define the request -->
<xs:complexType name="FormatRequest">
<xs:sequence>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>

<!-- Define the response -->
<xs:complexType name="FormatResponse">
<xs:sequence>
<xs:element name="formattedName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
(paste myschema.xsd file under src/main/resources/schema folder.)

Step-3.                                               applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
    default-dependency-check="none" default-lazy-init="false">

    <!-- Load the needed resources that are present in the cxf* jars -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <!-- Hook up the web service -->
    <jaxws:endpoint id="formatterService"
        implementor="com.cxftest.service.NameFormatterServiceImpl" address="/format" />

</beans>
(paste  applicationContext.xml file under src/main/resouces folder)

Step-4                                                           Write the pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cxftest</groupId>
    <artifactId>CXFTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>CXFTest</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cxf.version>2.1</cxf.version>
        <spring.version>2.5</spring.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-common-utilities</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step-5.                                        Create Java classes from xsd by using jaxb .

create one java package named com.cxf.jaxb under src/main/java and create java helper class by using Jaxb plugin using command prompt.
create helper classes by using jaxb maven plugin or by using eclipse ide..
By using Eclipse..
Right click on the xsd file in  project explorer.-->then click on generate-->click on jaxbClasess..
Now mention the package under which the jaxB classes will be created of that project.



it will generate the classes..

FormatRrequest.java
FormatResponse.java
ObjectFactory.java
package-info.java

These are the helper classes which are used to develop a web service application.

Step-6.                             Write the Business classes and interface.

NameFormatterService.java
package com.cxftest.service;

import com.cxf.jaxB.FormatRequest;
import com.cxf.jaxB.FormatResponse;


@WebService(targetNamespace="http://www.cxftest.org")
public interface NameFormatterService {
@WebMethod
public abstract FormatResponse formatName(FormatRequest request);

}
and its implementation class...

NameFormatterServiceImpl.java
package com.cxftest.service;

import com.cxf.jaxB.FormatResponse;




@WebService (targetNamespace="http://www.cxftest.org", endpointInterface="com.cxftest.service.NameFormatterService")
public class NameFormatterServiceImpl implements NameFormatterService {

/* (non-Javadoc)
* @see com.cxftest.service.NameFormatterService#formatName(com.cxftest.model.FormatRequest)
*/

@WebMethod
public FormatResponse formatName(FormatRequest request){
FormatResponse response = new FormatResponse();
response.setFormattedName(request.getLastName() +","+ request.getFirstName());
return response;
}
}

Step7.                              write  the web.xml file under WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Step-8

Build  the project By using maven.

   mvn - clean install.
 After build successful deploy it in tomcat or any other server and start the server...
launch the url using the below url..

http://localhost:8082/CXFTest-1.0-SNAPSHOT/format?wsdl

now you will be getting one link..like..
(it the service port .).
and click that...

Now you will be getting one file call WSDL file  which is the  web service contarct and can be used by any technolgy and any language to develop a web service clent application to invoke the bussness method..
 the WSDL is below..
<wsdl:definitions name="NameFormatterServiceImplService" targetNamespace="http://www.cxftest.org"><wsdl:types><xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://www.cxftest.org"><xs:element name="formatName" type="tns:formatName"/>
<xs:element name="formatNameResponse"
type="tns:formatNameResponse"/><xs:complexType name="formatName"><xs:sequence><xs:element minOccurs="0" name="arg0" type="tns:FormatRequest"/></xs:sequence></xs:complexType><xs:complexType name="FormatRequest">
<xs:sequence>
<xs:element form="qualified" name="firstName" type="xs:string"/>
<xs:element form="qualified" name="lastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="formatNameResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:FormatResponse"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="FormatResponse">
<xs:sequence>
<xs:element form="qualified" name="formattedName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="formatNameResponse">
<wsdl:part element="tns:formatNameResponse" name="parameters">
    </wsdl:part>
    </wsdl:message>
    <wsdl:message name="formatName">
    <wsdl:part element="tns:formatName" name="parameters">
    </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="NameFormatterService">
    <wsdl:operation name="formatName">
    <wsdl:input message="tns:formatName" name="formatName">
    </wsdl:input>
    <wsdl:output message="tns:formatNameResponse" name="formatNameResponse">
    </wsdl:output>
    </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="NameFormatterServiceImplServiceSoapBinding" type="tns:NameFormatterService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="formatName">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="formatName">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="formatNameResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="NameFormatterServiceImplService">
    <wsdl:port binding="tns:NameFormatterServiceImplServiceSoapBinding" name="NameFormatterServiceImplPort">
    <soap:address location="http://localhost:8082/CXFTest-1.0-SNAPSHOT/format"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>




                                                                                                                                             Continued...

                                   



No comments:

Post a Comment