Simple Web service client

Devloping  A CXF  web service client

A web service client can be devloped using the WSDL  file of the web service application.

Procedure to create a webservice client.

Step-1

Create a maven project  with directory like...
                            src/main/java
                            src/main/resources
                             src/main/webapp
create a pom.xml file under the project folder.

Step-2
Get WSDL file and paste it under src/main/resources and get the java classes from from that WSDL file by adding the following in you pom file..like..

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.marvelution.samples</groupId>
    <artifactId>spring-webservices-samples</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <url>http://maven.apache.org</url>
    <name>Cxf Client</name>

    <repositories>
   
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
       
        <repository>
            <id>java.net</id>
            <url>https://maven.java.net/content/repositories/public/</url>
        </repository>
    </repositories>

    <properties>
        <cxf.version>2.0.8</cxf.version>
    </properties>

    <dependencies>
        <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-bundle</artifactId>
            <version>${cxf.version}</version>
        </dependency>

  
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.3.04</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>2.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>2.5.5</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
       
        <dependency>
            <groupId>org.opensaml</groupId>
            <artifactId>opensaml</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.messaging.saaj</groupId>
            <artifactId>saaj-impl</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.security</groupId>
            <artifactId>wss4j</artifactId>
            <version>1.5.8</version>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.3.04</version>
        </dependency>
        <dependency>
            <groupId>org.apache.santuario</groupId>
            <artifactId>xmlsec</artifactId>
            <version>1.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.wss</groupId>
            <artifactId>xws-security</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>CxfClient</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
             <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
       
      </plugin>
      <plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>CxfClient/build/generated/cxf</sourceRoot>                               
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>CxfClient/src/main/wsdl/myService.wsdl</wsdl>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>
        </plugins>
    </build>
</project>

Build that and copy the java classes from  generated/cxf directory to your src/main/java packge directory by under one java package like me..




Step-2.
Create a simple servlet class that extends from HttpServlet class and will have service or doXXX method and write the code under that method like..
We can prepare the web service client in two ways.
  • with spring
  • without spring

  without spring


package com.cxf.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cxftest.FormatRequest;
import com.cxftest.NameFormatterService;

public class CxfClient extends HttpServlet{
    public void service(HttpServletRequest request, HttpServletResponse response) {

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        factory.setServiceClass(NameFormatterService.class);
        factory.setAddress("http://localhost:8082/CXFTest-1.0-SNAPSHOT/format?wsdl");
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());

       NameFormatterService client = (NameFormatterService) factory.create();
       FormatRequest student = new FormatRequest();
    student.setFirstName("Syntel Ltd");
    student.setLastName("It is a good company");
    /* Student changeName = client.changeName(student); */
    out.println("<b>Server said: " + student.getFirstName()+"</b><br>");
    out.println("<b>Server said: " + student.getLastName()+"</b><br>");
    out.println("<i>Welcome to india</i>");
    }
}

web.xml

 <?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">
 
 <servlet>
    <servlet-name>helloWs</servlet-name>
    <servlet-class>com.cxf.service.CxfClient</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloWs</servlet-name>
    <url-pattern>/HelloWs</url-pattern>
  </servlet-mapping>

</web-app>

and the output is 




With spring cxf.

package com.cxf.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cxftest.FormatRequest;
import com.cxftest.NameFormatterService;

public class CxfClient extends HttpServlet{
   
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{

PrintWriter out=response.getWriter();

ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");


NameFormatterService client = (NameFormatterService)context.getBean("client");

    FormatRequest student = new FormatRequest();
    student.setFirstName("Syntel Ltd");
    student.setLastName("It is a good company");
    /* Student changeName = client.changeName(student); */
    out.println("<b>Server said: " + student.getFirstName()+"</b><br>");
    out.println("<b>Server said: " + student.getLastName()+"</b><br>");
    out.println("<i>Welcome to india</i>");
}
}
and the spring configuration file is..
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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://cxf.apache.org/jaxws
      http://cxf.apache.org/schemas/jaxws.xsd">

  <bean id="proxyFactory"
    class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="com.cxftest.NameFormatterService"/>
    <property name="address" value="http://localhost:8082/CXFTest-1.0-SNAPSHOT/format"/>
  </bean>

  <bean id="client" class="com.cxftest.NameFormatterService"
    factory-bean="proxyFactory" factory-method="create"/>

</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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://cxf.apache.org/jaxws
      http://cxf.apache.org/schemas/jaxws.xsd">

  <bean id="proxyFactory"
    class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="com.cxftest.NameFormatterService"/>
    <property name="address" value="http://localhost:8082/CXFTest-1.0-SNAPSHOT/format"/>
  </bean>

  <bean id="client" class="com.cxftest.NameFormatterService"
    factory-bean="proxyFactory" factory-method="create"/>

</beans>
 and same web.xml file.



Step-4
Run the pom file with "mvn -e clean install" and deploy the war file in server and launch the url so that the request will directly go the servlet class..









No comments:

Post a Comment