Pages

mardi 16 avril 2013

WS Tutorial Part II JAX-WS webservice client




Before you start
About this tutorial

Objectives

In this section, you learn how to create web service clients from WSDL. JAX-WS comes with a tool called wsimport that's used to generate JAX-WS portable artifacts from WSDL. The portable artifacts typically generated include the following:


·         SEI
·         Service (the service implementation class you need to implement)
·         JAXB-generated classes from schema types
·         Exception class mapped from wsdl:fault (if any)

Clients use the artifacts generated to invoke the web service. The web service clients don't need to deal with any SOAP format, like creating or parsing SOAP messages. Rather, this is handled by the JAX-WS run time, which uses the generated artifact code (the JAXB-generated class). The web service client in turn deals with the Java object (the JAXB-generated class), which eases the development of web service clients and invoking operations on the web service.
  • We will build it with Maven
  • We will not copy jars to the Tomcat. All dependencies will be managed by Maven
Plan :
   1- Web service maven project
   2- Web services artifacts for client generation with wsimport
     3- A factory, a manager and a main class for test

Prerequisites
To complete this tutorial successfully, you should have a basic understanding of web services technology and have some proficiency in Java programming.

You should also have accomplished the first part of  the tutorial that you can find here : 
http://amaraaymen.blogspot.be/2012/10/creating-jax-ws-webservice-using-wsgen.html

System requirements

To run the examples in this tutorial, you need :
  • Eclipse WTP 3.4 
  • Maven 3.0 
  • JAXWS RT 2.1.3 
  • JAVA 6 
  • TOMCAT 6

1- Web service Client maven project

In command Line type to generate a java client project with maven using this commands :
1-      mvn archetype:create -DgroupId=com.aam.jaxws.client -DartifactId=PersonManagementClient
2-      mvn eclipse:eclipse => to be able to import it to eclipse


2- Generate web services artifacts for client with wsimport :

To generate artifacts we will use maven wsimport plugin, let’s update our pom.xml by adding this build fragment and execute mvn clean install command line under the PersonManagementClient folder.

 <build>
     <plugins>
       <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>jaxws-maven-plugin</artifactId>
               <version>1.10</version>
               <executions>
                      <execution>
                            <goals>
                                   <goal>wsimport</goal>
                            </goals>
                      </execution>
               </executions>
       <configuration>
       <packageName>com.aam.jaxws.client.generated</packageName>


<wsdlUrls>
  <wsdlUrl>http://localhost:8080/PersonManagementServer/PersonManagementService?wsdl       </wsdlUrl>
</wsdlUrls>
       <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
       <destDir>${basedir}/src/main/java</destDir>
       <phase>generate-sources</phase>
       <encoding>UTF-8</encoding>
       <extension>true</extension>
      </configuration>
   </plugin>
</plugins>
</build>

Maven wsimport pluggin has generated the artifiacts in the specified package :



3- A factory, a manager and a main class for test:

Let’s create a factory of our webservice that gives an instance of our Service whenever it’s needed.

package com.aam.jaxws.client;

import java.net.URL;
import javax.xml.namespace.QName;
import com.aam.jaxws.client.generated.PersonManagementService;
import com.aam.jaxws.client.generated.PersonManagementService_Service;

/**
 * @author AMARA Aymen
 */

public class PersonManagementServiceFactory {

       protected URL wsdlLocation = null;

       public PersonManagementServiceFactory(URL wsdlLocation) {
              this.wsdlLocation = wsdlLocation;
       }

       public PersonManagementService getPersonManagementService() {
              try {

PersonManagementService_Service serviceImplService = new PersonManagementService_Service (wsdlLocationnew QName("http://com.aam.jaxws.server","PersonManagementService") );

return serviceImplService .getPersonManagementServicePort();
              } catch (Exception e) {
                     return null;
              }
       }
}

Let’s create a manger that will use this factory and search for a person by CIN.

package com.aam.jaxws.client;
import java.net.URL;
import org.apache.log4j.Logger;
import com.aam.jaxws.client.generated.PersonException_Exception;
import com.aam.jaxws.client.generated.PersonManagementService;
import com.aam.jaxws.client.generated.PersonOut;

public class PersonManager {

       protected Logger log = Logger.getLogger(PersonManager.class);
       protected PersonManagementService personManagementService = null;

protected PersonManager(String wsdlLocation) {
try {
PersonManagementServiceFactory service = new PersonManagementServiceFactory(
                                  new URL(wsdlLocation));
this.personManagementService = service.getPersonManagementService();
catch (Exception e) {
log.error("Unable to initialize " + PersonManager.class.getName()+ ".", e);
}
       }

public PersonOut findPerson(String CIN) {

              try {
                     return personManagementService.findPersonByCIN(CIN);
              } catch (PersonException_Exception e) {
                     e.printStackTrace();
              }
              return null;

       }
}



A main class to call the webservice TestPersonManagementClient.java :

package com.aam.jaxws.client;

import com.aam.jaxws.client.generated.PersonOut;


public class TestPersonManagementClient {
       public static void main(String[] args) {
              String CIN = "p1";
              PersonManager manager = new PersonManager("http://localhost:8080/PersonManagementServer/PersonManagementService?wsdl");
             
PersonOut person = manager.findPerson("p1");

if (person != null)
       System.out.println("Person Found = firstName :"
                                  + person.getFirstName() + " Name:" + person.getName());

              else
                     System.out.println("Person with CIN =" + CIN + "ISNOT FOUND");
       }
}


Result  :

Person Found = firstName :aam Name:aam



As you see in the client code, you don't deal with any SOAP or XML-based format for invoking web service operations; instead you deal with generated JAXB classes for input and output messages and use the service interface and service class objects, which act as stubs for web service invocation. The stubs are responsible for creating SOAP requests from JAXB annotations and converting the SOAP response back to the Java object.

You can use this links to Download source code
  DOWNLOAD CLIENT PROJECT
  DOWNLOAD SERVER PROJECT

After downloading, execute mvn clean install command in both folders and  import projects to your eclipse. You can after deploy the WebService in your favorite server and run the main client.

END.