Utilisation d'une bibliothèque client
Une bibliothèque client est utilisée pour créer une requête de service Web pour HCL Commerce. Vous pouvez utiliser une bibliothèque de client de services HCL Commerce (par exemple, MemberFacadeClient) dans une application Java standard.
Avant de commencer
- Comprenez et sélectionnez le mécanisme d'authentification de votre bibliothèque client.
- Déployez la bibliothèque client dans votre environnement de développement à l'aide des instructions pour le mécanisme d'authentification que vous sélectionnez.
Pourquoi et quand exécuter cette tâche
L'utilisation d'une bibliothèque client dépend de la façon dont elle est invoquée :
- Utilisation de la balise GetData pour effectuer une requête de service ; par exemple, créer une requête Get à l'aide d'un JSP, applicable au Centre de gestion et aux clients du magasin.
- Utilisation du ComponentServiceAction ; par exemple, créer une requête de Change/Process à partir du magasin.
- Utilisation directe de la bibliothèque client ; par exemple, une application J2SE.
Pour utiliser une bibliothèque client :
Procédure
- Ajoutez le chemin d'accès du fichier JAR de la bibliothèque client au chemin d'accès de génération de votre application Java.
- Initialisez la bibliothèque client, comme dans l'exemple de code suivant :
private MemberFacadeClient iClient = null; /** * Sets the initializes the client based on the <code>CallbackHandler</code> * @param aCallbackHandler {@link CallbackHandler} */ private void initializeClient(CallbackHandler aCallbackHandler) { iClient = new MemberFacadeClient(iBusinessContext, aCallbackHandler); } - Utilisez la bibliothèque client, comme le prévoit l'exemple de code suivant pour un appel de service Get :
Un exemple d'appel d'un service Process ou Change est présent dans le code suivant ://Initialize the client with site admin user permissions initializeClient(new SampleCallbackHandlerImpl(SITE_ADMIN_LOGON_ID, SITE_ADMIN_PWD)); //Create the BOD GetType getType = AbstractBusinessObjectDocumentFacadeClient.createGetVerb( SelectionCriteriaHelper.STR_XPATH_LANG, "{" + MemberFacadeConstants.SELF + "=true;" + SelectionCriteriaHelper.STR_ACCESS_PROFILE_PARAMETER + "=" + MemberFacadeConstants.ACCESS_PROFILE_SUMMARY_INFORMATION + "}/Person"); //Use the client library to call the Get Person Web service ShowPersonDataAreaType showPersonDAT = iClient.getPerson(getType); // Get the PersonType from the response and use the printPerson() method to display the response data PersonType person = (PersonType) showPersonDAT.getPerson().get(0); // Output the logon ID System.out.println(person.getCredential().getLogonID());//Initialize the client to run as a guest user - use null as the callback handler initializeClient(null); Map parameters = new HashMap(); parameters.put("logonId", new String[]{ "myLogonId" } ); parameters.put("logonPassword", new String[]{ "myPassw0rd" }); parameters.put("lastName", new String[]{"myLastName"}); parameters.put("city", new String[]{"Toronto"}); try{ Map result = iClient.registerPerson(parameters); String [] strUserId = (String[]) result.get("userId"); System.out.println("========= New userId: " + strUserId[0]); } catch (PersonException e) { List listErrors = e.getClientErrors(); if (listErrors != null) { for (int i=0; i < listErrors.size(); i++) { ClientError clientError = (ClientError) listErrors.get(i); System.out.println("Message: " + clientError.getLocalizedMessage(Locale.ENGLISH)); System.out.println("Error key: " + clientError.getErrorKey()); System.out.println("Error code: " + clientError.getErrorCode()); } } } - Traitez la réponse selon votre application Java.