Métodos recomendados para correlacionar métodos de API REST con mandatos de controlador
Siga el ejemplo de código y las sugerencias para la codificación para asegurarse de que los servicios de mandato de controlador de REST nuevos o personalizados se ajustan a los métodos recomendados de HCL Commerce.
Procedimiento
-
El código de ejemplo siguiente invoca un mandato de controlador en el manejador de recursos de mandatos:
String cmdRefKey = OrderItemDeleteCmd.NAME; TypedProperty requestProperties = null; response = executeControllerCommandWithContext(storeId, cmdRefKey, requestProperties, responseFormat); // the response object here is already formatted and can be returned via Rest /** * This methods sets up the BCS by calling startRequest(), * executes the controller command, and then ends the BCS by calling * endRequest(BusinessContextService). * * @param storeId the store ID * @param cmdRefKey the command interface name * @param requestProperties the request properties * @param responseFormat the response format * @return the response */ protected Response executeControllerCommandWithContext(String, String, TypedProperty, String);
-
El código de ejemplo siguiente manipula los parámetros de mandato antes de que se ejecute el único mandato de controlador. Añada el siguiente código antes de llamar a
executeControllerCommandWithContext(String, String, TypedProperty, String)
:// if custom logic is needed to set command parameter prior to command // execution, here are some sample code to do so // for OrderCopyCmd, URL is a mandatory parameter TypedProperty requestProperties = initializeRequestPropertiesFromRequestMap(responseFormat); requestProperties.put("URL", "URL"); /** * This method creates a request properties from the request map. * * @param responseFormat the response format * @throws Exception the exception occurs */ protected TypedProperty initializeRequestPropertiesFromRequestMap(String) throws Exception;
-
El código de ejemplo siguiente ejecuta varios mandatos de controlador en un manejador de recursos personalizado:
// setting up the BCS BusinessContextService bcs = startRequest(); try { cmdRefKey = OrderCopyCmd.NAME; requestPropertiesForCopy = initializeRequestPropertiesFromRequestMap(responseFormat); requestPropertiesForCopy.put("URL", "URL"); // invoke the first controller command TypedProperty responsePropertiesOfCopy = executeControllerCommand(storeId, cmdRefKey, requestPropertiesForCopy, responseFormat); responseProperties = addToResponse(responseProperties, responsePropertiesOfCopy, "copyResponse"); cmdRefKey = OrderCalculateCmd.NAME; requestPropertiesForCal = new TypedProperty(); String[] orderIdList = (String[])responsePropertiesOfCopy.get("orderId"); for (int i=0; i<orderIdList.length; i++) { requestPropertiesForCal.put("orderId_"+(i+1), orderIdList[i]); } // invoke the next controller command TypedProperty responsePropertiesOfCal = executeControllerCommand(storeId, cmdRefKey, requestPropertiesForCal, responseFormat); responseProperties = addToResponse(responseProperties, responsePropertiesOfCal, "calculateResponse"); response = generateResponseFromHttpStatusCodeAndRespData(responseFormat, responseProperties.getMap(), HttpStatus.OK); // clean up the BCS endRequest(bcs); /** * This method associates the BCS with the activity data and the activity token by * calling BusinessContextService startRequest(ActivityToken, ActivityData) * to perform any necessary set up for request execution. * * @throws Exception the exception occurs */ protected BusinessContextService startRequest() throws Exception; /** * This method creates a command context, looks up the command implementation from the command * registry, associates the command context created and the request properties to the command. * It also performs command level and resource level access control check before invoking the * command. * * @param storeId the store ID * @param cmdRefKey the command interface name * @param requestProperties the request properties * @param responseFormat the response format * @return the response properties from the command * @throws Exception the exception occurs */ protected TypedProperty executeControllerCommand(String, String, TypedProperty, String) throws Exception; /** * This method generates REST response for a given response map. * * Use this method in your resource handler class when you want to pass name value pairs to * the entity provider. * * @param responseFormat the response format shortcut. Response format is used to resolve the * media type for the REST response. If response format is not provided, then Accept * header is used to resolve the media type for the REST response. Refer to {@link RestServicesUtils#getResponseMediaType(String, Request)} * for more details on the response media type resolution. * * @param responseData the response map to pass to the entity provider. * @param statusCode the HTTP status code to set in the REST response. * * @return the REST response. */ public Response generateResponseFromHttpStatusCodeAndRespData(String, Map<String, Object>, HttpStatus); /** * This method calls BusinessContextService endRequest(ActivityToken) * to perform any necessary cleanup after request execution. * * @param bcs the business context * @throws Exception the exception occurs */ protected void endRequest(BusinessContextService) throws Exception;
- Cada recurso REST contiene información como URL, descripciones y datos de entrada y salida de ejemplo en la API REST de HCL Commerce: