Web service client with Axis2 (using Axis Data Bindings)

The objective of this lab is to use/test the web service developed in the previous lab. To do so, we will develop an Axis client using the automatic Java/XML interface generation tools provided by this tool. We will use the WSDL2Java utility, which generates from the WSDL description of a service the different classes and client interfaces required to call this service on the client side.

The WSDL description of the HelloWorld example can be accessed at: http://localhost:8080/axis2/services/HelloWorldService?wsdl

  1. Create a new folder called ClientAxis2.
  2. Navigate to the created folder and generate the client Java interfaces for the HelloWorld service using wsdl2java.bat (for windows users) or wsdl2java.sh (for linux users) the following command, Do not forget to run set-env.bat in the new widow before:
    1. On Windows:
      >cd %TPWS%\ClientAxis2 
      >wsdl2java.bat -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -d adb
    2. On Linux:
      >cd $TPWS/ClientAxis2
      >wsdl2java.sh -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -d adb
  3. The WSDL2Java utility generates a number of Java classes and interfaces including the HelloWorldServiceStub.java class. This class contains the different methods exposed by this service. In the sense of the WSDL specification, this is a type of port («portType») that implements several “operations” (the methods of our service). The HelloWorldServiceStub.java class contains a class for each complex data type optionally defined by the service (in the .wsdl file).
  4. The client code depends on the generated classes. In the client code, it suffices to:
    1. instantiate the stub
    2. prepare the input parameters
    3. call a stub method

Here are two sample codes that should be created in the Client class, next to the “hello” folder, using different methods. The first code utilizes the stub method, while the second one uses the https method.

Client Class Code with Stub Method in the 'hello' Folder:

import hello.HelloWorldServiceStub;
import hello.HelloWorldServiceStub.SayHelloResponse;
public class Client {
   public static void main (String [] args){
       try{
           //creation d'un stub pour le service Web Hello World
           HelloWorldServiceStub stub = new HelloWorldServiceStub();
           //invocation de la methode hello
           SayHelloResponse resp = stub.sayHello();
           // affichage de resultat
           System.out.println(resp.get_return());
       }catch(Exception e){
           System.out.println(e);
       }
 
 
   }   
} 

Client Class Code with HTTPS Method in the 'hello' Folder:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpHeaders;
import java.util.List;
import java.util.Map;
 
public class HttpClientExample {
    public static void main(String[] args) {
        try {
            // Spécifiez l'URL du service web Hello World
            String serviceUrl = "http://localhost:8080/hello"; // Remplacez par l'URL réelle de votre service
 
            // Crée un client HTTP
            HttpClient client = HttpClient.newHttpClient();
 
            // Crée une requête HTTP GET avec l'URL du service
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(serviceUrl))
                    .GET()
                    .build();
 
            // Envoie la requête et reçoit la réponse
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
 
            // Affiche le code de statut de la réponse
            int statusCode = response.statusCode();
            System.out.println("Code de statut : " + statusCode);
 
            // Affiche l'en-tête de la réponse
            HttpHeaders headers = response.headers();
            Map<String, List<String>> headerMap = headers.map();
            headerMap.forEach((key, values) -> System.out.println(key + ": " + values));
 
            // Affiche le corps de la réponse (contenu)
            String responseBody = response.body();
            System.out.println("Corps de la réponse : " + responseBody);
        } catch (Exception e) {
            // Gère les exceptions
            e.printStackTrace();
        }
    }
} 
  1. Compile the java classes:
    >javac *.java   
  2. Run the client:
    >java Client