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
>cd %TPWS%\ClientAxis2 >wsdl2java.bat -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -d adb
>cd $TPWS/ClientAxis2 >wsdl2java.sh -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -d adb
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(); } } }
>javac *.java
>java Client